【问题标题】:Splitted pdf files are as large as the original pdf拆分的pdf文件与原始pdf一样大
【发布时间】:2018-09-10 22:13:32
【问题描述】:

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

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

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

这是我的代码:

use setasign\Fpdi\Fpdi;
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 文本。

提前感谢您的帮助:)

【问题讨论】:

    标签: php fpdf fpdi


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-09
      • 2016-08-04
      相关资源
      最近更新 更多