Spiga

PDF Generation with Zend Framework

November 07, 09 by Gabi Solomon
Jonathan MaronGuest Post by:
Jonathan Maron has been working in the web application conceptualization and development space since 1996. His current position is with an international company, specialized in the production of word processing components. Being an advocate of the FOSS movement, Jonathan promotes the use of Open Source software and rejects the notion of reinventing the wheel, preferring to develop with established frameworks.

Generating print-ready well-formatted PDF documents with PHP is not an easy task. Traditionally, there are two main approaches to PDF generation with PHP. Given sufficient time and patience, both partially get the job done, but still leave a lot to be desired:

HTML-to-PDF: This approach is widely used in mainstream applications. Here an HTML document is programmatically created and converted to a PDF, using one of the many open source libraries 1. Since HTML, however, is not a page-oriented format (as is PDF), it is impossible to perform a 1-to-1 mapping between HTML and PDF. Typical word processing file format features, such as header and footers, orphans and widows or even page numbers can simply not be represented in HTML.
Read the rest of this entry »

PHP – convert Microsoft word doc to pdf

September 08, 08 by Gabi Solomon

A lot of people are searching for solution to do this conversion on the fly. I dont know any other programing languages except PHP so i will write only about how you can do this in PHP.

Now to do this there is 2 major situations:
1. Doing it on a windows Server
2. Doing it on a linux Server

In both cases the chanses are that you need to have root access to the server in order to do all the settings required for this conversion.

In case of windows you can use COM objects to acces either Word or OpenOffice and do the conversion. Here peek of a code to access OpenOffice API to do the conversion:

PHP:
  1. <?php
  2. function MakePropertyValue($name,$value,$osm){
  3. $oStruct = $osm->Bridge_GetStruct("com.sun.star.beans.PropertyValue");
  4. $oStruct->Name = $name;
  5. $oStruct->Value = $value;
  6. return $oStruct;
  7. }
  8. function word2pdf($doc_url, $output_url){
  9. //Invoke the OpenOffice.org service manager
  10. $osm = new COM("com.sun.star.ServiceManager") or die ("Please be sure that OpenOffice.org is installed.\n");
  11. //Set the application to remain hidden to avoid flashing the document onscreen
  12. $args = array(MakePropertyValue("Hidden",true,$osm));
  13. //Launch the desktop
  14. $oDesktop = $osm->createInstance("com.sun.star.frame.Desktop");
  15. //Load the .doc file, and pass in the "Hidden" property from above
  16. $oWriterDoc = $oDesktop->loadComponentFromURL($doc_url,"_blank", 0, $args);
  17. //Set up the arguments for the PDF output
  18. $export_args = array(MakePropertyValue("FilterName","writer_pdf_Export",$osm));
  19. //Write out the PDF
  20. $oWriterDoc->storeToURL($output_url,$export_args);
  21. $oWriterDoc->close(true);
  22. }
  23. $output_dir = "C:/dev/openofficeintegration/docconverter/";
  24. $doc_file = "C:/dev/openofficeintegration/docconverter/DpmR5Reqv1.20.doc";
  25. $pdf_file = "DpmR5Reqv1.20.pdf";
  26. $output_file = $output_dir . $pdf_file;
  27. $doc_file = "file:///" . $doc_file;
  28. $output_file = "file:///" . $output_file;
  29. word2pdf($doc_file,$output_file);
  30. ?>

Now in case of linux things get a little more tricky, not by much but they do. You cant user COM objects any more but you can still install OpenOffice and its API in extension with a python script.

PyODConverter, for Python OpenDocument Converter, is a Python script that automates office document conversions from the command line using OpenOffice.org.

The script does basically the same thing as the command line tool that comes with JODConverter but is much simpler. In fact the Python script was released for the people who use JODConverter only from the command line (not as a Java library or web service) and would like a simpler alternative.

To check out the full documentation and download the script go here.

Covertor for php5

As i was doing research to do this post i found out of yet another solution, but this is only available for php5, but it works for both lynux and windows. Its a php module written in C++ called Punno.

This project is a PHP5 module written in C++ that brings the OpenOffice.org UNO Programming API to the PHP userspace.

You can use it to write scripts that create, modify, read and save OpenOffice.org documents (Writer, Spreadsheet, Drawing). Also, you can export these documents in various formats, like PDF or HTML for example.

It can be installed on any Linux/Unix or Windows platform where PHP5 and OpenOffice.org are also available.

It is released under PHP License 3.01.

That is it.

Do you know of any other solutions to do a doc to pdf conversion using php ?