Skip to content
Permalink
Browse files

Direct use of Mpdf

  • Loading branch information...
Thoulah committed Mar 17, 2019
1 parent b7b3a61 commit da9e47e949bd0537d4cfad20c834ee6848da8edf
@@ -26,7 +26,7 @@
"2amigos/yii2-usuario": "~1.0",
"codemix/yii2-localeurls": "^1.7",
"gkralik/php-uglifyjs": "^1.0",
"kartik-v/yii2-mpdf": "*",
"mpdf/mpdf": "^8.0",
"kop/yii2-scroll-pager": "*",
"nezhelskoy/yii2-highlight": "*",
"zenorocha/clipboardjs": "^2.0",

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -85,10 +85,6 @@ public function getValues(): array {
'encryption' => 'tls',
],
],
'pdf' => [
'class' => \kartik\mpdf\Pdf::class,
'mode' => \kartik\mpdf\Pdf::MODE_UTF8,
],
'urlManager' => [
'class' => 'codemix\localeurls\UrlManager',
'enablePrettyUrl' => true,
@@ -118,7 +114,6 @@ public function getValues(): array {
'music/lyrics/<artist:.*?>' => 'music/lyrics',
'music/collection-cover/<id:.*>.jpg' => 'music/collection-cover',
'articles/<id:\d+>/<title:.*?>.pdf' => 'articles/pdf',
$params['shortDomain'].'art<id:\d+>' => 'permalink/articles',
'articles/<id:\d+>/<title:.*?>' => 'articles/article',
'articles/<id:\d+>' => 'articles/article',
'articles/<action:create|update|delete>/<id:.*>' => 'articles/<action>',
@@ -131,6 +126,15 @@ public function getValues(): array {
'<alias:\w+>' => 'site/<alias>',
],
],
'urlManagerAssets' => [
'class' => 'codemix\localeurls\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'baseUrl' => $params['shortDomain'],
'rules' => [
'art<id:\d+>' => 'permalink/articles',
],
],
],
'language' => 'en',
'name' => 'Mr.42',
@@ -1,7 +1,7 @@
<?php
namespace app\models;
use Yii;
use kartik\mpdf\Pdf as PdfCreator;
use Mpdf\{Mpdf, HTMLParserMode};
use Mpdf\Pdf\Protection;
use Mpdf\Pdf\Protection\UniqidGenerator;
use Mpdf\Writer\BaseWriter;
@@ -17,21 +17,22 @@ public function create(string $filename, string $content, int $updated, array $p
if (!file_exists($filename) || filemtime($filename) < $updated) :
FileHelper::createDirectory(dirname($filename));
$pdf = new PdfCreator();
$pdf->api->SetCreator(Yii::$app->name);
$pdf->content = $content;
$pdf->filename = $filename;
$pdf->destination = PdfCreator::DEST_FILE;
$pdf = new Mpdf();
$pdf->SetCreator(Yii::$app->name);
foreach (['author', 'footer', 'header', 'keywords', 'subject', 'title'] as $x) :
if (isset($params[$x])) :
$function = 'Set'.ucfirst($x);
$pdf->api->$function($params[$x]);
$pdf->$function($params[$x]);
endif;
endforeach;
$pdf->render();
$writer = new BaseWriter($pdf->api, new Protection(new UniqidGenerator()));
$cssFile = Yii::getAlias('@runtime/assets/css/site.css');
$pdf->WriteHTML(file_get_contents($cssFile), HTMLParserMode::HEADER_CSS);
$pdf->WriteHTML($content, HTMLParserMode::HTML_BODY);
$pdf->Output($filename, \Mpdf\Output\Destination::FILE);
$writer = new BaseWriter($pdf, new Protection(new UniqidGenerator()));
$this->replaceLine($filename, '/Producer', $writer->utf16BigEndianTextString('Yii Framework'));
$this->replaceLine($filename, '/CreationDate', $writer->string(date('YmdHis', $created).substr(date('O', $created), 0, 3)."'".substr(date('O', $created), 3, 2)."'"));
$this->replaceLine($filename, '/ModDate', $writer->string(date('YmdHis', $updated).substr(date('O', $updated), 0, 3)."'".substr(date('O', $updated), 3, 2)."'"));
@@ -91,13 +91,13 @@ public static function buildPdf(Articles $model): string {
$pdf = new Pdf();
return $pdf->create(
'@runtime/PDF/articles/'.sprintf('%05d', $model->id),
$model->contentParsed,
str_replace('[readmore]', null, $model->contentParsed),
$model->updated,
[
'author' => $name,
'created' => $model->created,
'footer' => implode('|', ["{$tags}: {$model->tags}", "Author: {$name}", 'Page {PAGENO} of {nb}']),
'header' => implode('|', [Html::a(Yii::$app->name, Url::to(['site/index'], true)), Html::a($model->title, ['/permalink/articles', 'id' => $model->id]), date('D, j M Y', $model->updated)]),
'header' => implode('|', [Html::a(Yii::$app->name, Yii::$app->params['shortDomain']), Html::a($model->title, Yii::$app->urlManagerAssets->createUrl(['/permalink/articles', 'id' => $model->id])), date('D, j M Y', $model->updated)]),
'keywords' => $model->tags,
'subject' => $model->title,
'title' => $model->title,
@@ -63,7 +63,7 @@ public static function buildPdf(self $album): string {
Lyrics3Tracks::getLastModified($album->artist->url, $album->year, $album->url),
[
'author' => $album->artist->name,
'footer' => implode('|', [Html::a(Yii::$app->name, Url::to(['site/index'], true)), $album->year, 'Page {PAGENO} of {nb}']),
'footer' => implode('|', [Html::a(Yii::$app->name, Yii::$app->params['shortDomain']), $album->year, 'Page {PAGENO} of {nb}']),
'header' => implode('|', [$album->artist->name, 'Lyrics', $album->name]),
'keywords' => implode(', ', [$album->artist->name, $album->name, 'lyrics']),
'subject' => implode(' - ', [$album->artist->name, $album->name]),
@@ -56,7 +56,7 @@
echo Html::tag('div', $author, ['class' => 'card-footer']);
echo Html::beginTag('div', ['class' => 'card-footer']);
$bar[] = Yii::$app->icon->show('link', ['class' => 'mr-1 text-muted']).Html::a(Yii::t('mr42', 'Permalink'), ['/permalink/articles', 'id' => $model->id]);
$bar[] = Yii::$app->icon->show('link', ['class' => 'mr-1 text-muted']).Html::a(Yii::t('mr42', 'Permalink'), Yii::$app->urlManagerAssets->createUrl(['/permalink/articles', 'id' => $model->id]));
$commentText = Yii::t('mr42', '{results, plural, =0{no comments yet} =1{1 comment} other{# comments}}', ['results' => count($model->comments)]);
$bar[] = Yii::$app->icon->show('comment', ['class' => 'mr-1 text-muted']).Html::a($commentText, ['article', 'id' => $model->id, 'title' => $model->url, '#' => 'comments']);
@@ -57,7 +57,7 @@
endforeach;
$doc->startElement('guid');
$doc->writeAttribute('isPermaLink', 'true');
$doc->text(Url::to(['permalink/articles', 'id' => $article->id]));
$doc->text(Yii::$app->urlManagerAssets->createUrl(['/permalink/articles', 'id' => $article->id]));
$doc->endElement();
$doc->writeElement('pubDate', date(DATE_RSS, $article->created));
if ($article->source) :
@@ -2,7 +2,7 @@
use app\models\music\Lyrics2Albums;
use yii\bootstrap4\Html;
echo '<bookmark content="track list" />';
echo '<bookmark content="tracklist" />';
echo '<br><br><br>';
echo '<div class="text-center">';

0 comments on commit da9e47e

Please sign in to comment.
You can’t perform that action at this time.