PHP-如何使用mPDF合并PDF [英] PHP - How to use mPDF to merge PDFs

查看:456
本文介绍了PHP-如何使用mPDF合并PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我可以使用mPDF生成PDF,但是为了我的生命,我无法将现有的PDF与刚刚生成的PDF合并.

I will start out by saying that I can generate PDFs just fine with mPDF, but for the life of me, I can't get it to merge an existing PDF with the PDF it just generated.

我需要弄清楚的是如何将现有的PDF附加/添加到新生成的PDF中.我尝试使用mPDF方法导入页面,但是我只能得到一个错误,例如:

What I need to figure out is how to append/add the existing PDF to the newly generated PDF. I've tried using the mPDF methods for importing pages, but all I can get is an error like:

mPDF error: Cannot open '/downloads/test.pdf'.

上面的消息模棱两可,不清楚为什么无法打开文件... 这是我用来尝试合并PDF的代码:

The above message is ambiguous and unclear as to WHY it can't open the file... Here's the code I'm using to try and merge PDFs:

 include_once("./pdf/mpdf/mpdf.php");

 $output_file = $_GET['output_file'];
 $url = $_GET['input_file'];
 $technical_drawing = $_GET['tech_drawing'];

 $html = file_get_contents($url);

 $mpdf = new mPDF('utf-8','Letter','','',0,0,0,0,0,0,'P');
 $mpdf->SetImportUse(); 

 $pagecount = $mpdf->SetSourceFile($technical_drawing);
 $tplIdx = $mpdf->ImportPage($pagecount);
 $mpdf->UseTemplate($tplIdx);

 $mpdf->WriteHTML($html);
 $mpdf->Output($output_file, 'D');

 exit;

$ output_file将是显示给用户的文件名. $ url是我们在PDF生成期间正在写入文件的HTML. $ technical_drawing是PDF的相对路径,我们要与生成的PDF附加/合并.

$output_file will be the filename shown to the user. $url is the HTML we're writing into the file during PDF generation. $technical_drawing is a relative path to a PDF that we want to append/merge with the generated PDF.

我知道我可以使用诸如ghostscript之类的东西,但是我在客户端的服务器上没有这种访问权限.

I understand I could use something like ghostscript, but I don't have that type of access on the client's server.

让我知道是否有人使用mPDF找到了解决方案,或者我是S.O.L..并且需要找到另一个库来进行PDF合并.我确实在寻找解决方案或建议,而不仅仅是链接到另一个库.我已经用尽了在Google或mPDF的文档中找到的描述我所遇到的错误的内容.

Let me know if anyone has found a solution to this using mPDF or if I'm S.O.L. and need to find another library to do PDF merging. I am really looking for solutions or suggestions, but not just links to another library. I've exhausted what I can find in Google or in mPDF's documentation that describes the error I'm having.

将mPDF错误从 http://example.com/pdf/example.pdf 更改为"/downloads" /test.pdf".

changed mPDF error from http://example.com/pdf/example.pdf to '/downloads/test.pdf'.

EDIT_2:代码已固定为采用相对路径.

EDIT_2: Code has been fixed to take relative path.

这是最终的工作代码.如果有人知道如何指定将HTML写入PDF文档的顺序,将页面导入为最后一页(其自定义页面大小与HTML的页面大小不同),则将获得奖励.

Here's final working code. Bonus if anyone knows how to specify the order of writing HTML to PDF document, importing page as last page (with custom page size different from that of the HTML).

    include_once("./pdf/mpdf/mpdf.php");

    $output_file = 'test-' . $_GET['output_file'];
    $url = $_GET['input_file'];
    $technical_drawing = $_GET['tech_drawing'];

    $html = file_get_contents($url);


    if(!file_exists($technical_drawing)) {
      $mpdf = new mPDF('utf-8','Letter','','',0,0,0,0,0,0,'L');
    } else {
      $mpdf = new mPDF('utf-8','A3-L','','',0,0,0,0,0,0,'L');

      $mpdf->SetImportUse(); 

      $pagecount = $mpdf->SetSourceFile($technical_drawing);
      $import_page = $mpdf->ImportPage();

      $mpdf->UseTemplate($import_page);

      // Add Last page
      $mpdf->AddPageByArray(array(
        'orientation' => 'P',
        'ohvalue' => 1,
        'ehvalue' => -1,
        'ofvalue' => -1,
        'efvalue' => -1,
        'newformat' => 'Letter'
      ));
    }

    $mpdf->WriteHTML($html);
    $mpdf->Output($output_file, 'D');

    exit;

推荐答案

我像这样合并pdf-{所有文件中的所有页面}:

I Merge pdfs like this - {all pages in all files}:

$ this =扩展mPDF类的类...

$this = Class that extends mPDF class...

function mergePDFFiles(Array $filenames, $outFile)
{
    if ($filenames) {

        $filesTotal = sizeof($filenames);
        $fileNumber = 1;

        $this->SetImportUse(); // this line comment out if method doesnt exist

        if (!file_exists($outFile)) {
            $handle = fopen($outFile, 'w');
            fclose($handle);
        }

        foreach ($filenames as $fileName) {
            if (file_exists($fileName)) {
                $pagesInFile = $this->SetSourceFile($fileName);
                for ($i = 1; $i <= $pagesInFile; $i++) {
                    $tplId = $this->ImportPage($i); // in mPdf v8 should be 'importPage($i)'
                    $this->UseTemplate($tplId);
                    if (($fileNumber < $filesTotal) || ($i != $pagesInFile)) {
                        $this->WriteHTML('<pagebreak />');
                    }
                }
            }
            $fileNumber++;
        }

        $this->Output($outFile);

    }

}

这篇关于PHP-如何使用mPDF合并PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆