简体   繁体   English

如何使用 JsBarcode.js 生成条形码并在 Yii 框架 2.0 中使用 Kartik mPDF 将其打印在 PDF 文件上

[英]How can I generate barcode with JsBarcode.js and print it on a PDF file using Kartik mPDF within Yii framework 2.0

I have installed Kartik mPDF extension within Yii framework 2.0 .我已经在Yii framework 2.0安装了Kartik mPDF扩展。 Below is the code snippet within my controller that generates PDF file and send it to the browser.下面是我的控制器中生成 PDF 文件并将其发送到浏览器的代码片段。

// setup kartik\mpdf\Pdf component
    $pdf = new Pdf([
        'mode' => Pdf::MODE_CORE, 
        'format' => Pdf::FORMAT_A4, 
        'orientation' => Pdf::ORIENT_PORTRAIT, 
        'destination' => Pdf::DEST_BROWSER, 

        // your html content input
        'content' => $this->renderPartial('print-barcode'),  

        'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
        'cssInline' => '.kv-heading-1{font-size:18px}', 
        'options' => ['title' => 'My PDF file'],
        'methods' => [ 
            'SetHeader'=>['My header'], 
            'SetFooter'=>['{PAGENO}'],
        ]
    ]);

    return $pdf->render(); 

I have two JS files ( JsBarcode.js and CODE128.js ) which can be found here https://github.com/lindell/JsBarcode To generate barcode I need to include these JS files and my custom JavaScript code.我有两个 JS 文件( JsBarcode.jsCODE128.js ),可以在这里找到https://github.com/lindell/JsBarcode要生成条形码,我需要包含这些 JS 文件和我的自定义 JavaScript 代码。 In my view, I normally include those files as follows (my print-barcode.php ).在我看来,我通常包括这些文件如下(我的print-barcode.php )。

<?php
$this->registerJsFile(Yii::$app->urlManager->baseUrl . '/js/JsBarcode.js', ['depends' => [\yii\web\JqueryAsset::className()]]);
$this->registerJsFile(Yii::$app->urlManager->baseUrl . '/js/Code128.js', ['depends' => [\yii\web\JqueryAsset::className()]]);

$jsGenerateBarcode = 'My custom Javascript code goes here ...';
$this->registerJs($jsGenerateBarcode, $this::POS_END);
?>

All the included JavaScript code does not have any effect at all, since it is a PDF file and not a webpage.所有包含的 JavaScript 代码根本没有任何效果,因为它是 PDF 文件而不是网页。 How can I print the barcode generated by JsBarcode.js and by CODE128.js and pass it on the PDF file which is generated by kartik\\mpdf\\Pdf;如何打印由JsBarcode.jsCODE128.js生成的条形码并将其传递到由CODE128.js kartik\\mpdf\\Pdf;生成的 PDF 文件中kartik\\mpdf\\Pdf; ? ?

Sorry so late, but this works for me.抱歉这么晚了,但这对我有用。 You should create a view with the information first, then use renderPartial and sent that to mpdf.您应该首先使用信息创建一个视图,然后使用 renderPartial 并将其发送到 mpdf。

My View contains:我的视图包含:

<script src="<?=yii::$app->params['rootUrl']?>/js/JsBarcode.code128.min.js"></script>

<p>Safety is Everyone's Responsibility</p>
<p><barcode code="<?=$model->qrcode?>" type="C128A" class="barcode" size="0.8" /></p>

And to test:并测试:

return $this->renderPartial('_print-view',['model'=>$myModel]);

And Finally to print the pdf (I was printing Badges so i was playing with margins):最后打印pdf(我正在打印徽章,所以我在玩边距):

$mpdf = new \Mpdf\Mpdf([  
//'mode' => 'c'
'margin_left' => 0,
'margin_right' => 0,
'margin_top' => 0,
'margin_bottom' => 0,
'format' => [54,86],
]);

$mpdf->SetTitle('Badge: '.$myModel->badge_number);
$mpdf->WriteHTML($this->renderPartial('_print-view',['model'=>$myModel,'page'=>1]));

$mpdf->AddPage('L');
$mpdf->WriteHTML($this->renderPartial('_print-view',['model'=>$myModel,'page'=>2]));

$mpdf->Output($badge_number,'I');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM