[英]Spliting pdf file into multi pdf files with same look and feel
I am using the following php function to split a pdf file into two files according to page number. 我正在使用以下php函数根据页码将pdf文件分为两个文件。 The source file has 14 pages.
源文件有14页。 I am creating the two files: First 12 pages for one and the last two pages for the second fils.
我正在创建两个文件:前一个文件的前12页,第二个文件的后两页。 But I am getting a different site format for the both created files than the source file.
但是我为两个创建的文件都获得了与源文件不同的站点格式。 How can I improve the function to make sure that I am getting the exact site format as the source file?
如何改善功能以确保获得与源文件相同的网站格式? I am using the pdf-reader library.
我正在使用pdf阅读器库。 Or what could you recomment to use as tool to reach this?
或您建议使用什么作为工具来达到此目的?
function split_pdf14($filename, $end_directory = false, $numberOfPages = 12, $row) {
$pdf = new \TCPDI();
$pagecount = $pdf->setSourceFile($filename);
$pagesDone = 0;
if ($pagecount > $numberOfPages) {
$maxFiles = floor($pagecount / $numberOfPages);
for ($j = 1; $j <= 2; $j++) {
$new_pdf = new \TCPDI();
$new_pdf->setSourceFile($filename);
for ($i = 1; $i <= $numberOfPages; $i++) {
$pagesDone++;
if ($pagesDone > $pagecount) {
/*
* do nohting
*/
} else {
$new_pdf->AddPage();
$new_pdf->useTemplate($new_pdf->importPage($pagesDone));
}
}
try {
if ($j == 1) {
$new_filename = 'localfiles/' . $row['f_auftragsnr'] . '_' . $row['t_sorte'] . '_' . $row['t_sprache'] . '_' . $row['t_umfang'] . '.pdf';
} else {
$new_filename = 'localfiles/' . $row['f_auftragsnr'] . '_' . $row['t_sorte'] . '_' . $row['t_sprache'] . '_' . $row['t_umfang'] . '_BON.pdf';
}
//$new_pdf->Output($new_filename, "F");
$new_pdf->Output($_SERVER['DOCUMENT_ROOT'] . '/' . $new_filename, 'F');
//$new_pdf->Output('/home/box/public_html/box2019/' . $new_filename, 'F');
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
$numberOfPages = $pagecount;
}
}
}
You are using the default page format (A4) and orientation. 您正在使用默认的页面格式(A4)和方向。
Your code doesn't care about the size of the imported pages and simply adds A4 portrait pages: 您的代码不关心导入页面的大小,而只添加A4纵向页面:
$new_pdf->AddPage();
$new_pdf->useTemplate($new_pdf->importPage($pagesDone));
You simply have to add the pages in the correct size and orientation: 您只需要以正确的尺寸和方向添加页面即可:
$tplId = $new_pdf->importPage($pagesDone);
$size = $this->getTemplatesize($tplId);
$new_pdf->AddPage($size['w'] > $size['h'] ? 'L' : 'P', [$s['w'], $s['h']]);
$new_pdf->useTemplate($tplId);
PS: The script you are using (TCPDI) seems to be several years old. PS:您正在使用的脚本(TCPDI)似乎已经使用了几年。 You may use the latest official version of FPDI for such task.
您可以将FPDI的最新正式版本用于此类任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.