5

I am currently trying to convert an ODT to PDF using OO silently from PHP. Here is the code for that:

function MakePropertyValue($name, $value,$osm){ 
   $oStruct = $osm->Bridge_GetStruct("com.sun.star.beans.PropertyValue"); 
   $oStruct->Name = $name; 
   $oStruct->Value = $value; 
   return $oStruct; 
} 
function odt2pdf($doc_url, $output_url){
$osm = new COM("com.sun.star.ServiceManager") or die ("Please be sure that OpenOffice.org is installed.\n");
$args = array(MakePropertyValue("Hidden",true,$osm));
$oDesktop = $osm->createInstance("com.sun.star.frame.Desktop");
$oWriterDoc = $oDesktop->loadComponentFromURL($doc_url,"_blank", 0, $args);
$aFilterData  = array();
$aFilterData [0] = $osm->Bridge_GetStruct("com.sun.star.beans.PropertyValue");
$aFilterData [0]->Name = "SelectPdfVersion";
$aFilterData [0]->Value = 1;
$obj  = $osm->Bridge_GetValueObject();
$obj->set("[]com.sun.star.beans.PropertyValue",$aFilterData );
$storePDF = array();
$storePDF[0] = $osm->Bridge_GetStruct("com.sun.star.beans.PropertyValue");
$storePDF[0]->Name = "FilterName";
$storePDF[0]->Value = "writer_pdf_Export";
$storePDF[1] = $osm->Bridge_GetStruct("com.sun.star.beans.PropertyValue");   
$storePDF[1]->Name = "FilterData";
$storePDF[1]->Value = $obj;
$oWriterDoc->storeToURL($output_url,$storePDF);
$oWriterDoc->close(true);
}
$output_dir = "C:/wamp/www/cert/pdf/";
$doc_file = "C:/wamp/www/cert/output2.odt";
$pdf_file = "output2.pdf";
$output_file = $output_dir . $pdf_file;
$doc_file = "file:///" . $doc_file;
$output_file = "file:///" . $output_file;
odt2pdf($doc_file,$output_file);

I have managed to get it to convert to PDF/A-1a as can be seen, however it still does not preserve the fonts being used. If I convert this from within the GUI in Open Office, then the fonts are kept. What's wrong?

UPDATE: I decided to stop converting to PDF and do a silent print of the odt to the printer with Open Office using exec() in PHP.

As an additional note, make sure to switch off Font substitution on the printer driver to prevent any font substitution when printing to a PostScript printer.

Resurgent
  • 525
  • 2
  • 9
  • 20
  • 3
    When mentioning OO, please refer to Open Office if that's what you mean. Because I think it's easy to think you mean "Object Oriented" when addressing an object oriented programming language. – Ms01 Aug 17 '12 at 11:01
  • how about running open/libre office cmd tool? libreoffice --headless --invisible --nologo --nofirststartwizard --convert-to pdf --outdir testx old_file.odt – Juris Malinens Aug 26 '12 at 14:55

1 Answers1

0

Try by first converting the ODT to HTML ( to maintain formatting ) by using this library

https://gist.github.com/1918801

and later use this FPDF library to convert from html to pdf

I guess FPDF wont support so easily to convert from html so there is a plugin for that FPDF

here is the link for the plugin

plugin for FPDF to convert from HTML

Aravind.HU
  • 9,194
  • 5
  • 38
  • 50