【问题标题】:How to convert HTML into PDF in Magento 2?如何在 Magento 2 中将 HTML 转换为 PDF?
【发布时间】:2017-05-16 01:26:03
【问题描述】:

我想在 Magento 2 的自定义模块中将 HTML 转换为 PDF。我不想使用 zend PDF。 Magento 2 中还有其他方法可以将 HTML 转换为 PDF 吗?

【问题讨论】:

    标签: php magento pdf pdf-generation magento2


    【解决方案1】:

    我的一位同事创建了一个您可以使用的 DomPDF 模块,请参阅https://github.com/weprovide/magento2-module-dompdf

    【讨论】:

    • 嗨,我如何在销售订单网格的默认发票打印操作中使用它。?请指导这样做..
    【解决方案2】:

    第 1 步(安装库)

    composer require setasign/fpdf:1.8.1 && composer require setasign/fpdi:2.0 && composer require knplabs/knp-snappy && composer require h4cc/wkhtmltopdf-amd64 "0.12.4"
    

    第 2 步

    <?php
    
    namespace vendor\module\Helper\DPD;
    
    require_once(BP.'/vendor/setasign/fpdf/fpdf.php');
    require_once(BP.'/vendor/setasign/fpdi/src/autoload.php');
    use \setasign\Fpdi\Fpdi;
    use Knp\Snappy\Pdf;
    
    
    class Data extends \Magento\Framework\App\Helper\AbstractHelper
    {
    public function getLabel( $shipmentId, $orderModel, $parcelNumbers ){
    
                try{
                    $accountNumber = 'testing;
                    $headers = array(
                        'Content-Type: application/json',
                        'Accept: text/html',
                        'GeoClient: account/' . $accountNumber,
                        'GeoSession: ' . $loginSession);
                    $apiUrl = "URL here";
                    $content = $this->apiCall($headers,$apiUrl, 'GET');
                   
    //                 echo $content;exit;
                    $directory = 'path to save the file';
                    $fileName = $orderModel->getIncrementId() .".pdf";
                    try {
    
                        $snappy = new Pdf(\h4cc\WKHTMLToPDF\WKHTMLToPDF::PATH);
                        $FileNAme = $fileName;
                        $fullUrl = $directory . '/' . $FileNAme;
                        if (file_exists($fullUrl)) {
                            unlink($fullUrl);
                        }
                        $snappy->generateFromHtml($content, $fullUrl, array(
                            'orientation' => 'portrait',
                            'page-width' => '107',
                            'dpi' => 12399,
                            'page-height' => '107',
                            'lowquality' => false,
                            'margin-bottom'  => 0,
                            'margin-left'  => 0,
                            'margin-right'  => 0,
                            'margin-top'  => 0,
                            'zoom' => 1.23,
                            'encoding' => 'utf-8'
                        ));
    
                        $pdf = new FPDI('L','mm', array(107,107));
                        $pageCount = $pdf->setSourceFile($fullUrl); //the original file
                        for ($i = 1; $i <= $pageCount; $i++) {
                            $pageformat = array('Rotate' => 180);
                            $tpage = $pdf->importPage($i);
                            $size = $pdf->getTemplateSize($tpage);
                            // get original page orientation
                            $orientation = 'portrait';
                            $pdf->AddPage($orientation, '', 180);
                            $pdf->useTemplate($tpage);
                        }
                        $pdf->Output($fullUrl, "F");
                        $data = file_get_contents($fullUrl);
    
                    } catch(Exception $e) {
                        $Error = "Error Occur while getting the label from API : " . $e->getMessage();
                        $this->dpdErrors[] = $Error;
                        $this->mbDpdLog($Error);
                    }
    
                }catch( exception $e ){
                }
            }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-01-12
      • 2017-08-11
      • 1970-01-01
      • 2017-05-06
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多