Pregunta

¿Hay alguna librería PHP PDF que puede sustituir las variables de marcador de posición en un PDF existente, documento ODT o DOCX, y generar un archivo PDF como el resultado final, sin atornillar el diseño?

Requisitos:

  • No necesita tercera parte del servicio web

  • Capacidad para ejecutar el alojamiento web compartido sería ideal (no hay instalaciones binarios / paquetes requeridos)

Eso sí, una biblioteca que es capaz de cargar un archivo PDF y texto inserto existente mediante programación en una posición específica es no es suficiente para mi caso de uso.

En cuanto a mis programas de investigación, no existe una biblioteca que puede hacer esto:

  • TCPDF sólo puede generar documentos desde cero

  • FPDI puede leer las plantillas PDF existente, pero sólo puede añadir contenidos mediante programación (sin reemplazo variable de plantilla)

  • Hay varias bibliotecas de plantillas DOCX / ODT por ahí, pero no lo hacen PDF de salida

PHPDOCx dice ser capaz de hacer exactamente lo que necesito - pero no ofrecen una versión de prueba y no estoy va a comprar un gato en una bolsa, sobre todo cuando parece que hay ningún otro producto en la web que hace esto. Me resulta difícil de creer que pueden hacer esto sin problemas -. Si usted ha hecho esto con éxito el uso del producto, por favor, deje caer una línea aquí

estoy pasando por alto algo?

¿Hay una manera de hacer esto utilizando formularios PDF? Estoy creando los documentos fuente de OpenOffice 3.

puede ser capaz de utilizar los comandos estándar de Linux (pdftk está disponible, por ejemplo, tratando de que fuera en este momento.)

Actualización: * Argh * me llamaron de la oficina y la generosidad expiró mientras tanto!. Inicio de una nueva recompensa:. En lo que a mi muestra de prueba, ninguna solución funciona perfectamente para mí todavía

Actualización II: voy a buscar el enfoque pdftk pronto, pero también estoy empezando otra recompensa para una ronda más de recogida de entrada adicional. Esta pregunta se ha visto 1300 puntos representante de las recompensas, debe haber algún tipo de un registro:)

¿Fue útil?

Solución

Pekka,

I looked in to this previously, I think you can use pdftk (a command line utility), to fill in a PDF form using FDF/XFDF data files, which you could easily generate from within PHP. That was the best option I've seen so far, though there may well be a native library.

pdftk is quite useful in general, worth having a look at.

Update: Have a look here: http://php.net/manual/en/book.fdf.php

Otros consejos

This is not very practical, but for completeness: If you already have an ODT template, then you might very well retain that as template. Modifying the OpenDocument content.xml and replacing placeholders therein is pretty simple. If so, you could use unoconv or pyodconverter to transform the ODT into a final PDF.

unoconv -f pdf -o final.pdf template.odt

Very obviously this requires a full OpenOffice setup (UNO and Writer) on the webserver. And obviously not every webhoster would go with that! haha. Even if it's simple on any Debian or Fedora setup. The execution speed would probably not be stellar either. But then it might be the cleanest approach, since OOo governs both formats way better than any PHP class ever could.

Have you considered using something like XSL:Formatting Objects (XSL:FO)? Basically they're XML documents that are processed and turned into PDFs. Doing string - or better, DOM - replacements within that should be pretty simple. It supports embedding images, links, annotations, etc.

It's not PHP but there are a number of PHP wrappers for it along with ways of using it via exec, etc. Not an ideal but it takes care of the template portion completely. For some more info: http://techportal.inviqa.com/2009/12/16/transforming-xml-with-php-and-xsl/

There's an implementation available as an Apache project - http://xmlgraphics.apache.org/fop/

fpdf and there is another extention on top of it, which I can't remember, which allows you to import templates

Your best bet would be to generate the entire document on the fly, with the template defined programatically using fpdf or something similar. That way, your text will not be cut off by paragraphs or anything like that, and you can easily position images/other elements as required.

Late, but you can use OpenSource template designer https://github.com/applicius/dhek/releases , to define pkaceholders/areas over any existing PDF, then load it in PHP (as it's JSON format) and write accordingly on original PDF using fpdf lib, to generate custom PDF with dynamic data written on.

Altough not exactly thing you asked, you may consider to make it at two steps: using some php templating sytem (smarty, dwoo) to generate html page and then using tools like Html2Pdf convert it to pdf. I am using it, and results are good (no problems with page layout etc)

Of course it depends of your input documents (can you use html instead of PDF/ ODT as source ) and complexity of the layout of those.

Ok I'm trying to help you solve the problem a little.

First the answer for couple of your question.

Q - Am I overlooking something?

A - No. There is a PHP PDF library that can replace placeholder variables in an existing PDF and generate a PDF file as the end result, without screwing up the layout

Q - Is there a way to do this using PDF forms?

A - Yes. absolutelly the tric to doing this is by using a PDF Forms

For both answer you can use Justin Koivisto fill pdf form field php library. For more detail you please go to http://koivi.com/fill-pdf-form-fields/tutorial.php. Take a look there for additional information.

Credit to Justin Koivisto for his work

P.S

For workaround for displaying a table like output from pdf form please consider to take some reading on Oracle Business Intelligence Publisher User's Guide - Creating a PDF Template

I'll add this new answer since the FDF PHP extension is now dead.

I've just followed these instructions and ended up executing one perl script then the pdftk command

I'm pretty aware it's far from being a real PHP solution but it's reliable and fairly easy to implement on any *nix platform.

The tools described there are also available on Debian, just in case you were wondering.

It's a litte bit late but have a look at the PDFTemplate Library it does exatly what you want. You can create Open Document files (odt) and add placeholders in it. The PDFTemplate library can fill out these placeholders (even with images) and create a PDF file.

ODT Files with placeholders to PDF

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top