开发者

Create new PDF from template with PHP

开发者 https://www.devze.com 2023-03-15 02:02 出处:网络
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.

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.

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 stackoverflow. 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.

Creating a P开发者_运维百科DF 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.

Are there no easy way of using a template like there is for Excel with 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


I really recommend 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

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


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.

TCPD examples


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

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?

Writing/Drawing over a PDF template document in PHP


http://www.livedocx.com/

LiveDocx is a template-based document creation platform.

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


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!


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.

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.

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


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.


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


If your server has OpenOffice installed you can use soffice in headless mode to convert RTF-Files (Rich Text Format) to PDF.

  1. Create RTF with template brackets inside Text like "Dear, {{{name}}}, how are you?"

  2. Open the file and load the content with $content=file_get_contents('path_to_file');

  3. Replace the text as you wish: $content=str_replace('{{{name}}}', 'Bruce Wanye', $content);

  4. Save the content to a new file: file_put_contents('path_to_file2', $content);

  5. Now convert to PDF: excec("soffice --headless --convert-to pdf 'path_to_file2' --outdir 'target_directory'");


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

$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');
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号