使用 PHP 从模板创建新的 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6438522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:23:39  来源:igfitidea点击:

Create new PDF from template with PHP

phppdf-generationhtml2pdf

提问by Kim

I have a docx document file set up with formating and layout, which I saved as a PDF file. I want to use this pdf file as a template for creating new pdf files pre-filled with user data.

我有一个设置了格式和布局的 docx 文档文件,我将其保存为 PDF 文件。我想使用这个 pdf 文件作为模板来创建预先填充用户数据的新 pdf 文件。

I have seen FPDF, FPDI, tcPDF, domPDF, Zend PDF, http://koivi.com/fill-pdf-form-fields/(Justin Koivisto), a few convertion tools like HTML2PDFand a few command line tools mentioned here on stackoverflow. With the exception of FPDI, none of them offer an exampleusing a PDF template to either search-and-replace custom tags with actual data (usually from a database) or add data without specifying a bunch of values like x, y, postion, font, font-formating etc.

我见过FPDFFPDItcPDFdomPDFZend PDFhttp://koivi.com/fill-pdf-form-fields/(Justin Koivisto)、一些像HTML2PDF这样的转换工具和一些这里提到的命令行工具堆栈溢出。随着外FPDI,他们没有提供一个例子使用PDF模板,无论是搜索和替换与实际数据的自定义标签(通常是从一个数据库)或不指定一堆像X,Y,现在的位置值的添加数据,字体,字体格式等。

Creating a PDF from scratch seems like too much work. I have not yet considered using a HTML2PDFconverter as the results appear to vary greatly, and then there is the issue with styling. Some tools allow CSS, but often with limits like no float support.

从头开始创建 PDF 似乎工作量太大。我还没有考虑使用HTML2PDF转换器,因为结果似乎差别很大,然后还有样式问题。一些工具允许使用 CSS,但通常有一些限制,比如没有浮动支持。

Are there no easy way of using a template like there is for Excel with PHPExcel?

有没有像 Excel 和PHPExcel那样使用模板的简单方法?

My need is:

我的需求是:

  • Free
  • Load and use a template
  • Search-and-replace custom tags
  • Add data (text, image, font, formating etc)
  • Custom header and footer
  • Must work with shared hosting
  • Allow download, save/store to server and attach to email (preferable without having to save it first) of final generated PDF
  • 自由
  • 加载和使用模板
  • 搜索和替换自定义标签
  • 添加数据(文本、图像、字体、格式等)
  • 自定义页眉和页脚
  • 必须使用共享主机
  • 允许下载、保存/存储到服务器并附加到最​​终生成的 PDF 的电子邮件(最好无需先保存)

回答by drakezilla

I really recommend MPDF:

我真的推荐MPDF

  1. it is based on FPDF and HTML2FPDF, so you can use some of their functions or add others
  2. it has tons of cool features (create any kind of barcodes, locale support, watermarks, and many more)
  3. it has an extremely well made documentation
  4. it has a community forum
  5. it has examples!
  6. it has an easy learning curve
  1. 它基于 FPDF 和 HTML2FPDF,因此您可以使用它们的某些功能或添加其他功能
  2. 它有很多很酷的功能(创建任何类型的条形码、语言环境支持、水印等等)
  3. 它有一个制作精良的文档
  4. 它有一个社区论坛
  5. 它有例子!
  6. 它有一个简单的学习曲线

The only bad thing is that it is a little heavy-weight, but you can remove things that you dont need.

唯一不好的是它有点重,但是你可以删除你不需要的东西。

回答by Catalin Marin

use TCPDF you can create the PDF template using HTML tags principles it is fast to create, easy to use, easy to integrate I used it for generating detailed invoices of a law office(multiple pages with different format), the production envelope of a jewellery workshop (included photo), stock notes and invoices.

使用 TCPDF 您可以使用 HTML 标签原则创建 PDF 模板 创建速度快,易于使用,易于集成 我用它来生成律师事务所的详细发票(多页不同格式),珠宝的生产信封车间(包括照片)、库存票据和发票。

TCPD examples

TCPD 示例

回答by Gaurav Sharma

try FPDIit support editing an existing template and making changes in that template file

尝试FPDI它支持编辑现有模板并在该模板文件中进行更改

usage is pretty stratight forward :

用法非常直接:

<?php
require_once('fpdf.php');
require_once('fpdi.php');

$pdf = new FPDI();

$pagecount = $pdf->setSourceFile('TestDoc.pdf');
$tplidx = $pdf->importPage(1, '/MediaBox');

$pdf->addPage();
$pdf->useTemplate($tplidx, 10, 10, 90);

$pdf->Output('newpdf.pdf', 'D');

Also refer to links below: Is there a solution for pdf template in PHP?

另请参阅以下链接: PHP 中的 pdf 模板是否有解决方案?

Writing/Drawing over a PDF template document in PHP

在 PHP 中编写/绘制 PDF 模板文档

回答by Felipe Duardo

回答by ju1ce--

The problem with trying to use a PDF as a "template" is that PDF is a typeset document format. Eg. it is a fixed layout and you shouldn't really mess with the layout too much.

尝试使用 PDF 作为“模板”的问题在于 PDF 是一种排版文档格式。例如。这是一个固定的布局,你不应该把布局弄得太多。

It would be much simpler, if you want to do more with this, to use something like XSL-FO to generate your documents from an XML "template". All you need do is generate the XML with the right fields in place and the XSL-FO renderer can then create the PDF for you.

如果您想对此做更多事情,使用 XSL-FO 之类的东西从 XML“模板”生成文档会简单得多。您需要做的就是生成带有正确字段的 XML,然后 XSL-FO 渲染器可以为您创建 PDF。

The best (only?) free XSL-FO renderer is Apache FOP.

最好的(唯一的?)免费的 XSL-FO 渲染器是 Apache FOP。

回答by cchantep

You can skip all the pain of defining template over existing PDF with https://github.com/applicius/dhek/releases. It creates a JSON mapping area over PDF page/coordinates, so that you can fill it with PDF API you prefer.

您可以使用https://github.com/applicius/dhek/releases跳过在现有 PDF 上定义模板的所有痛苦。它在 PDF 页面/坐标上创建一个 JSON 映射区域,以便您可以使用您喜欢的 PDF API 填充它。

回答by NotJay

Instead of doing this with PHP, you could use Word or OpenOffice Mail Merge feature to create the template and import the data into the tags. Then you can "print" but export as a pdf instead of actually printing. This might be a faster solution provided you don't need to update the template regularly. I did this for about 150 products and it took less than 30 minutes including creating the template and uploading the pdfs. Just an after-thought, hope this helps somebody else!

您可以使用 Word 或 OpenOffice Mail Merge 功能来创建模板并将数据导入标签中,而不是使用 PHP 来执行此操作。然后您可以“打印”但导出为 pdf 而不是实际打印。如果您不需要定期更新模板,这可能是一个更快的解决方案。我为大约 150 种产品执行了此操作,包括创建模板和上传 pdf 在内的时间不到 30 分钟。只是事后的想法,希望这对其他人有所帮助!

回答by glerendegui

I was needing that for a project so I've created a fast tool for that. You can check it and feel free to contribute here

我在一个项目中需要它,所以我为此创建了一个快速工具。你可以检查它并随时在这里做出贡献

回答by user3222082

Take a look at http://template2pdf.com/It provides an API that allows you to use OpenOffice or LibreOffice templates and do substitution of text or images.

看看http://template2pdf.com/它提供了一个 API,允许您使用 OpenOffice 或 LibreOffice 模板并替换文本或图像。

回答by Sergey Onishchenko

Here is some example how to create multiple pages with one PDF template and content as HTML. Based on MPDFlibrary.

下面是一些如何使用一个 PDF 模板和 HTML 内容创建多个页面的示例。基于MPDF库。

$mpdf = new mPDF('utf-8', $format_size, 0, '', $margin_left, $margin_right, $margin_top, $margin_bottom, 0, 0, $orientation);

if ( $bg_template ) { 
    // is a PDF template file
    $mpdf->SetImportUse();
    $pagecount = $mpdf->SetSourceFile( $bg_template );
    $tplIdx = $mpdf->ImportPage( $pagecount );
}

foreach ( $html as $one_letter_text ) {
    $mpdf->addPage();

    if ( $bg_template ) {
        $mpdf->useTemplate( $tplIdx, 0, 0);
    }

    $mpdf->WriteHTML( $one_letter_text );
}
$title = 'my-letter.pdf';

$pdf_path = $this->created_pdf_upload_dir.'/'.$title;
$mpdf->Output( $pdf_path, 'F');