使用PHP从模板创建新的PDF

时间:2022-04-20 05:42:57

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 HTML2PDF and a few command line tools mentioned here on *. With the exception of FPDI, none of them offer an example using 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.

我见过FPDF,FPDI,tcPDF,domPDF,Zend PDF,http://koivi.com/fill-pdf-form-fields/(Justin Koivisto),一些像HTML2PDF这样的转换工具和这里提到的一些命令行工具堆栈溢出。除了FPDI之外,它们都没有提供使用PDF模板来搜索和替换自定义标签与实际数据(通常来自数据库)或添加数据而不指定一堆值(如x,y,postion)的示例字体,字体格式等

Creating a PDF from scratch seems like too much work. I have not yet considered using a HTML2PDF converter 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 ?

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

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的电子邮件(最好不必先保存)

9 个解决方案

#1


13  

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. 它基于FPDF和HTML2FPDF,因此您可以使用它们的一些功能或添加其他功能

  3. it has tons of cool features (create any kind of barcodes, locale support, watermarks, and many more)
  4. 它有很多很酷的功能(创建任何类型的条形码,区域设置支持,水印等等)

  5. it has an extremely well made documentation
  6. 它有一个非常好的文档

  7. it has a community forum
  8. 它有一个社区论坛

  9. it has examples!
  10. 它有例子!

  11. it has an easy learning curve
  12. 它有一个简单的学习曲线

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

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

#2


6  

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

#3


4  

try FPDI it 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模板文档

#4


2  

http://www.livedocx.com/

LiveDocx is a template-based document creation platform.

LiveDocx是一个基于模板的文档创建平台。

http://www.phplivedocx.org/articles/getting-started-with-phplivedocx/

#5


1  

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。

#6


1  

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填充它。

#7


1  

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种产品做了这个,花了不到30分钟,包括创建模板和上传pdf。只是一个想法,希望这有助于其他人!

#8


0  

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模板并替换文本或图像。

#9


0  

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

以下是如何使用一个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');

#1


13  

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. 它基于FPDF和HTML2FPDF,因此您可以使用它们的一些功能或添加其他功能

  3. it has tons of cool features (create any kind of barcodes, locale support, watermarks, and many more)
  4. 它有很多很酷的功能(创建任何类型的条形码,区域设置支持,水印等等)

  5. it has an extremely well made documentation
  6. 它有一个非常好的文档

  7. it has a community forum
  8. 它有一个社区论坛

  9. it has examples!
  10. 它有例子!

  11. it has an easy learning curve
  12. 它有一个简单的学习曲线

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

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

#2


6  

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

#3


4  

try FPDI it 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模板文档

#4


2  

http://www.livedocx.com/

LiveDocx is a template-based document creation platform.

LiveDocx是一个基于模板的文档创建平台。

http://www.phplivedocx.org/articles/getting-started-with-phplivedocx/

#5


1  

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。

#6


1  

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填充它。

#7


1  

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种产品做了这个,花了不到30分钟,包括创建模板和上传pdf。只是一个想法,希望这有助于其他人!

#8


0  

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模板并替换文本或图像。

#9


0  

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

以下是如何使用一个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');