Generate PDF from .docx generated by PHPWord
Asked Answered
F

4

16

I am creating .docx files from a template using PHPWord. It works fine but now I want to convert the generated file to PDF.

First I tried using tcpdf in combination with PHPWord

$wordPdf = \PhpOffice\PhpWord\IOFactory::load($filename.".docx");

\PhpOffice\PhpWord\Settings::setPdfRendererPath(dirname(__FILE__)."/../../Office/tcpdf");
\PhpOffice\PhpWord\Settings::setPdfRendererName('TCPDF');

$pdfWriter = \PhpOffice\PhpWord\IOFactory::createWriter($wordPdf , 'PDF');
if (file_exists($filename.".pdf")) unlink($filename.".pdf");
$pdfWriter->save($filename.".pdf");  

but when I try to load the file to convert it to PDF I get the following exception while loading the file

Fatal error: Uncaught exception 'BadMethodCallException' with message 'Cannot add PreserveText in Section.'

After some research I found that some others also have this bug (phpWord - Cannot add PreserveText in Section)

EDIT

After trying around some more I found out, that the Exception only occurs when I have some mail merge fields in my document. Once I removed them the Exception does not come up anymore, but the converted PDF files look horrible. All style information are gone and I can't use the result, so the need for an alternative stays.



I thought about using another way to generate the PDF, but I could only find 4 ways:

  1. Using OpenOffice - Impossible as I cannot install any software on the Server. Also going the way mentioned here did not work either as my hoster (Strato) uses SunOS as the OS and this needs Linux
  2. Using phpdocx - I do not have any budget to pay for it and the demo cannot create PDF
  3. Using PHPLiveDocx - This works, but has the limitation of 250 documents per day and 20 per hour and I have to convert arround 300 documents at once, maybe even multiple times a day
  4. Using PHP-Digital-Format-Convert - The output looks better than with PHPWord and tcpdf, but still not usable as images are missing, and most (not all!) of the styles

Is there a 5th way to generate the PDF? Or is there any solution to make the generated PDF documents look nice?

Footbridge answered 12/10, 2015 at 14:53 Comment(4)
Decent PDF output will probably require you to create a second template (you've already got a MSWord template, next you'll want a PDF template taking your source document to the input that tcpdf wants).Mycah
@Mycah Yeah I already thought about creating a HTML template as there are some librarys to convert HTML to PDF, but I have quite a lot different templates, so it would be quite a lot of work to create them and rewrite the way I fill them with content, so I hope there is another more easy way...Footbridge
This link might help: github.com/PHPOffice/PHPWord/issues/311Siberson
PHPWord should do the job, like @Siberson mentioned. As you can't install additional software there shouldn't be any other option to convert it to a nice lookin PDF. My other thought would be using Imagick but this requires the soffice binary which shouldn't be installed on strato...Nankeen
P
1

I used Gears/pdf to convert the docx file generated by phpword to PDF:

$success = Gears\Pdf::convert(
            'file_path/file_name.docx',
            'file_path/file_name.pdf');
Panathenaea answered 19/2, 2018 at 7:45 Comment(1)
Gears is no longer active as of 2022 unfortunatelyDenti
D
1

You're trying to unlink the PDF file before saving it, and you have also to unlink the DOCX document, not the PDF one.

Try this.

$pdfWriter = \PhpOffice\PhpWord\IOFactory::createWriter($wordPdf , 'PDF');    
$pdfWriter->save($filename.".pdf");
unlink($wordPdf);
Dewy answered 26/9, 2018 at 14:11 Comment(0)
A
0

I don't think I'm correct.. You save the document as HTML content

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');

After than you read the HTML file content and write the content as PDF file with the help of mPDF or tcPdf or fpdf.

Acyl answered 23/6, 2017 at 5:10 Comment(0)
C
0

Try this:

// get the name of the input PDF
$inputFile = "C:\\PHP\\Test1.docx";

// get the name of the output MS-WORD file
$outputFile = "C:\\PHP\\Test1.pdf";

try
    {
    $oLoader = new COM("easyPDF.Loader.8");
    $oPrinter = $oLoader->LoadObject("easyPDF.Printer.8");
    $oPrintJob = $oPrinter->PrintJob;
    $oPrintJob->PrintOut ($inputFile, $outputFile);
    print "Success";
    }


catch(com_exception $e)
    {
    Print "error code".$e->getcode(). "\n";
    print $e->getMessage();
    }
Counterpoise answered 20/11, 2018 at 4:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.