Rapport-Laravel/public/createpdf/testpdf.php

82 lines
2.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
require_once __DIR__ . '/vendor/autoload.php';
if ($argc < 5) {
fwrite(STDERR, "Usage: php testpdf.php <width_px> <height_px> <image_path> <output_pdf> [url] [link_x] [link_y] [link_w] [link_h]\n");
exit(1);
}
$widthPx = (float) $argv[1];
$heightPx = (float) $argv[2];
$imagePath = $argv[3];
$outputPath = $argv[4];
$url = $argv[5] ?? '';
if (!file_exists($imagePath)) {
fwrite(STDERR, "Image not found: {$imagePath}\n");
exit(1);
}
$imagePath = realpath($imagePath) ?: $imagePath;
$url = trim(html_entity_decode($url, ENT_QUOTES | ENT_HTML5, 'UTF-8'));
$url = str_replace(
["\xE2\x81\x84", "\xE2\x88\x95", "\xEF\xBC\x8F", "\xE2\xA7\xB8", '/', '⁄', '∕'],
'/',
$url
);
if ($url !== '' && !preg_match('#^https?://#i', $url)) {
$url = 'https://' . ltrim($url, '/');
}
$widthMm = max(1, $widthPx * 25.4 / 96);
$heightMm = max(1, $heightPx * 25.4 / 96);
$pxToMm = static function (float $px): float {
return $px * 25.4 / 96;
};
$mpdf = new \Mpdf\Mpdf([
'format' => [$widthMm, $heightMm],
'margin_left' => 0,
'margin_right' => 0,
'margin_top' => 0,
'margin_bottom' => 0,
'margin_header' => 0,
'margin_footer' => 0,
'allow_local_files' => true,
]);
$escapedImage = htmlspecialchars($imagePath, ENT_QUOTES, 'UTF-8');
$imgTag = '<img src="' . $escapedImage . '" style="width:' . $widthMm . 'mm;height:' . $heightMm . 'mm;margin:0;padding:0;border:0;display:block;" />';
$html = '<!DOCTYPE html><html><head><meta charset="utf-8"></head>';
if ($url !== '') {
$escapedUrl = htmlspecialchars($url, ENT_QUOTES, 'UTF-8');
$hasLinkRect = $argc >= 9;
if ($hasLinkRect) {
$linkLeftMm = $pxToMm((float) $argv[6]);
$linkTopMm = $pxToMm((float) $argv[7]);
$linkWidthMm = max(1, $pxToMm((float) $argv[8]));
$linkHeightMm = max(1, $pxToMm((float) $argv[9]));
$html .= '<body style="margin:0;padding:0;position:relative;">'
. $imgTag
. '<a href="' . $escapedUrl . '" style="position:absolute;left:' . $linkLeftMm . 'mm;top:' . $linkTopMm
. 'mm;width:' . $linkWidthMm . 'mm;height:' . $linkHeightMm . 'mm;display:block;"></a>';
} else {
$html .= '<body style="margin:0;padding:0;">'
. '<a href="' . $escapedUrl . '" style="display:block;line-height:0;text-decoration:none;">' . $imgTag . '</a>';
}
} else {
$html .= '<body style="margin:0;padding:0;">' . $imgTag;
}
$html .= '</body></html>';
$mpdf->WriteHTML($html);
$mpdf->Output($outputPath, \Mpdf\Output\Destination::FILE);