我想在Magento 2的自定义模块中将HTML转换为PDF。我不想使用zend PDF。在Magento 2中有没有其他方法将HTML转换为PDF?
kcrjzv8t1#
我的一个同事创建了一个您可以使用的DomPDF模块,请参见https://github.com/weprovide/magento2-module-dompdf
9gm1akwq2#
步骤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 ){ } } }
2条答案
按热度按时间kcrjzv8t1#
我的一个同事创建了一个您可以使用的DomPDF模块,请参见https://github.com/weprovide/magento2-module-dompdf
9gm1akwq2#
步骤1(安装库)
步骤2