拆分的 pdf 文件与原始 pdf 文件一样大



我有一个用FPDF生成的150Mb pdf(55页,包含文本和图像(。

我想将此PDF拆分为单页PDF。

我使用 FPDI,但我有一个主要问题,每页 PDF 都是 150Mb(就像原始 pdf 一样(。

这是我的代码:

use setasignFpdiFpdi;
require('fpdf181/fpdf.php');
require('fpdi/autoload.php');
function split_pdf($filename, $end_directory = false)
{
$end_directory = $end_directory ? $end_directory : './';
$new_path = preg_replace('/[/]+/', '/', $end_directory.'/'.substr($filename, 0, strrpos($filename, '/')));
if (!is_dir($new_path))
{
// Will make directories under end directory that don't exist
// Provided that end directory exists and has the right permissions
mkdir($new_path, 0777, true);
}
$pdf = new FPDI();
$pagecount = $pdf->setSourceFile($filename); // How many pages?
// Split each page into a new PDF
for ($i = 1; $i <= $pagecount; $i++) {
$new_pdf = new FPDI();
$new_pdf->AddPage();
$new_pdf->setSourceFile($filename);
$templateIndex = $new_pdf->importPage($i);
$new_pdf->useTemplate($templateIndex, null, null, 0, 0, true);
try {
$new_filename = $end_directory.str_replace('.pdf', '', $filename).'_'.$i.".pdf";
$new_pdf->Output($new_filename, "F");
echo "Page ".$i." split into ".$new_filename."<br />n";
} catch (Exception $e) {
echo 'Caught exception: ',  $e->getMessage(), "n";
}
}
}
// Create and check permissions on end directory!
split_pdf("contract.pdf", 'split/');

我的原始PDF只嵌入PNG和Helvetica文本。

提前感谢您的任何帮助:)

FPDF 使用单个资源字典,这意味着所有资源(如图像、字体或其他导入的页面(通过 FPDI((都位于一个地方。页面引用此字典作为资源源,无论资源是否在特定页面上使用。

FPDI 只是在导入页面时复制资源字典,包括所有定义的资源。它不会分析页面内容来决定哪些资源可以忽略。

用FPDI解决这个问题是不可能的(只要有人为此编写扩展(。

对于任何合并或拆分PDF文档的工具来说,此问题都是一个常见问题。我们(Setasign - 也是FPDI的作者(在另一个合并/拆分工具上也遇到了这个问题,但是我们能够编写一个脚本来优化资源。也许这个解决方案可能会帮助你。看看这里。此解决方案不是免费的。

最新更新