title: Generate PDF from .docx generated by PHPWord
body: <p>I am creating <code>.docx</code> files from a template using <code>PHPWord</code>. It works fine but now I want to convert the generated file to <code>PDF</code>.</p>
  
  <p>First I tried using <code>tcpdf</code> in combination with <code>PHPWord</code></p>
  
  <pre><code>$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-&gt;save($filename.".pdf");  
  </code></pre>
  
  <p><s>but when I try to load the file to convert it to <code>PDF</code> I get the following exception while loading the file</p>
  
  <blockquote>
    <p>Fatal error: Uncaught exception 'BadMethodCallException' with message 'Cannot add PreserveText in Section.'</s></p>
  </blockquote>
  
  <p><s>After some research I found that some others also have this bug (<a href="https://stackoverflow.com/questions/33070424/phpword-cannot-add-preservetext-in-section">phpWord - Cannot add PreserveText in Section</a>)</s></p>
  
  <p><strong>EDIT</strong></p>
  
  <blockquote>
    <p>After trying around some more I found out, that the <code>Exception</code> only occurs when I have some <code>mail merge fields</code> in my document. Once I removed them the <code>Exception</code> does not come up anymore, but the converted <code>PDF</code> files look horrible. All style information are gone and I can't use the result, so the need for an alternative stays.</p>
  </blockquote>
  
  <p><br><br></p>
  
  <p>I thought about using another way to generate the PDF, but I could only find 4 ways:</p>
  
  <ol>
  <li><strong>Using <a href="https://www.openoffice.org/" rel="noreferrer">OpenOffice</a></strong> - Impossible as I cannot install any software on the Server. Also going the way mentioned <a href="http://geekswithblogs.net/robertphyatt/archive/2011/11/19/converting-.docx-to-pdf-or-.doc-to-pdf-or-.doc.aspx" rel="noreferrer">here</a> did not work either as my hoster (<a href="https://www.strato.de/" rel="noreferrer">Strato</a>) uses SunOS as the OS and this needs Linux</li>
  <li><strong>Using <a href="http://www.phpdocx.com/" rel="noreferrer">phpdocx</a></strong> - I do not have any budget to pay for it and the demo cannot create PDF</li>
  <li><strong>Using <a href="http://www.phplivedocx.org/" rel="noreferrer">PHPLiveDocx</a></strong> - 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</li>
  <li><strong>Using <a href="https://github.com/benskay/PHP-Digital-Format-Convert-Epub-Mobi-PDF" rel="noreferrer">PHP-Digital-Format-Convert</a></strong> - The output looks better than with <code>PHPWord</code> and <code>tcpdf</code>, but still not usable as images are missing, and most (not all!) of the styles</li>
  </ol>
  
  <p>Is there a 5th way to generate the PDF? Or is there any solution to make the generated PDF documents look nice?</p>