1. Install dompdf
composer require dompdf/dompdf
2. Add Svg to Pdf
Caculate $dataQrs
$dataQrs = ImageHelper::imageToBase64($qrPathProduct)
Function imageToBase64
public static function imageToBase64($path)
{
$path = $path;
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
return $base64;
}
3. Custom font family for Pdf
Custom fontDir, fontCache
/**
* Generate QR code pdf
* @return array<string> images qr
*/
public function generateQRCodePdf($dataQrs)
{
// render pdf from $dataQrs
$html = $this->templating->render('@admin/Product/qr_code_template_pdf.twig', ['dataQrs' => $dataQrs]);
// set option for dompdf
$options = new Options([
"rootDir" =>"vendor/dompdf/dompdf",
"fontDir" =>"var/cache/qr/fonts", // path for auto generate font
"fontCache" =>"var/cache/qr/fonts", // path for auto generate font
"logOutputFile" =>null,
"defaultMediaType" =>"screen",
"defaultPaperSize" =>"a4",
"defaultPaperOrientation" =>"portrait",
"defaultFont" =>"Noto Sans",
"dpi" =>120,
"fontHeightRatio" =>1.1,
"isPhpEnabled" =>false,
"isRemoteEnabled" =>true, // have to true to generate font
"isJavascriptEnabled" =>true,
"isHtml5ParserEnabled" =>true,
"isFontSubsettingEnabled" =>false,
"debugPng" =>false,
"debugKeepTemp" =>false,
"debugCss" =>false,
"debugLayout" =>false,
"debugLayoutLines" =>true,
"debugLayoutBlocks" =>true,
"debugLayoutInline" =>true,
"debugLayoutPaddingBox" =>true,
"pdfBackend" =>"PDFLib",
"pdflibLicense" =>""
]);
$dompdf = new Dompdf($options);
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
// store temp_qr_pdf for this user
$filePdf = QRCodeSizes::QR_PATH . '/' . $this->handler->getId() . 'qr_temp.pdf';
$filePath = $this->eccubeConfig['eccube_save_image_dir'] . '/' . $filePdf;
$fileUrl = $this->assetPackage->getUrl($filePdf, 'save_image');
file_put_contents($filePath, $dompdf->output());
return $fileUrl;
}
load file font-family file .twig add style
ex for url: http://eccube.test/html/upload/qr/fonts/MeiryoJP-Bold.ttf (have to has http://)
<style>
@font-face {
font-family: 'Meiryo UI'; src: url({{ eccube_config.app_url ~ '/html/upload/qr/fonts/MeiryoJP-Bold.ttf' }}) format("truetype");
font-weight: 1200;
font-style: normal;
}
body {
font-family: "Meiryo UI";
}
</style>
or in header
<<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-type" content="application/xhtml+xml; charset=utf-8" />
<link href="https://fonts.googleapis.com/css2?family=Montserrat&display=swap" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css2?family=Tangerine&display=swap" rel="stylesheet" />
<style>
.m {
font-family: 'Montserrat';
}
.t {
font-family: 'Tangerine';
}
</style>
in body
<p class="m">xin chao helo メールの書式は正しくない</p>
<p class="t">xin chao helo メールの書式は正しくない</p>
Reference:
- https://github.com/dompdf/dompdf
* Explain option in Option of pdf
- $isRemoteEnabled = true => can use font-face, and font cache can auto create when you create pdf
/**
* Enable remote file access
*
* If this setting is set to true, DOMPDF will access remote sites for
* images and CSS files as required.
*
* ==== IMPORTANT ====
* This can be a security risk, in particular in combination with isPhpEnabled and
* allowing remote html code to be passed to $dompdf = new DOMPDF(); $dompdf->load_html(...);
* This allows anonymous users to download legally doubtful internet content which on
* tracing back appears to being downloaded by your server, or allows malicious php code
* in remote html pages to be executed by your server with your account privileges.
*
* This setting may increase the risk of system exploit. Do not change
* this settings without understanding the consequences. Additional
* documentation is available on the dompdf wiki at:
* https://github.com/dompdf/dompdf/wiki
*
* @var bool
*/
private $isRemoteEnabled = false;
- $isFontSubsettingEnabled" should set = true (default is true): isFontSubsettingEnabled is a configuration option that, when enabled, instructs the system to subset fonts, including only the characters used within the content, thereby reducing file size and improving performance.
Thank you
No comments:
Post a Comment