首页 文章

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

提问于
浏览
0

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

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

我使用FPDI,但我有一个主要问题,每页单页PDF是150Mb(juste就像原来的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."
\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文本 .

在此先感谢任何帮助:)

1 回答

  • 1

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

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

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

    对于组合或拆分PDF文档的任何工具,此问题是一个常见问题 . 我们(Setasign - 也是FPDI的作者)也有另一个合并/拆分工具的问题,但我们能够编写一个优化资源的脚本 . 也许这个解决方案可以帮到你 . 请看here . 这个解决方案不是免费的 .

相关问题