I used laravel for my app and the dompdf is found in:
../vendor/dompdf/dompdf
What I wanted to achieve is to convert my .docx file (Microsoft Word) to .pdf file. The docx file was generated by phpWord by loading the template file and replacing the values.
Here's the snippet:
// Get the absolute path of the template file
$wordTemplatePath = $this->getDocumentTemplatePath('resignation.docx');
// Load the template file
$document = $this->phpWord->loadTemplate($wordTemplatePath);
// This will be filled with data
$wordData = []
.... Process to fill the data .....
// Replace value to actual word document
$this->setTemplateValues($wordData, $document);
// Generate its filename
$file = $this->generateFileName('Retirement-Certificate.docx', $id);
// Fetch the absolute path to save the document
$filepath = $this->getSavePath($file);
// Save the word document
$document->saveAs( $filepath );
After that, a .docx file will be generated. I wanted to make a PDF version of that as well. so I search and found this code snippet and added in my code:
\PhpOffice\PhpWord\Settings::setPdfRendererPath('../vendor/dompdf/dompdf');
\PhpOffice\PhpWord\Settings::setPdfRendererName('DOMPDF');
//Load temp file
$phpWord = \PhpOffice\PhpWord\IOFactory::load($filepath);
//Save it
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'PDF');
$xmlWriter->save('result.pdf');
But I got this error:
{"error":
{"type":"PhpOffice\\PhpWord\\Exception\\Exception","message":"PDF rendering library or library path has not been defined.",
"file":"C:\\xampp\\htdocs\\vagrant\\vendor\\phpoffice\\phpword\\src\\PhpWord\\Writer\\PDF.php",
"line":49}}
How would I make DomPDF as my pdf writer for PHPWord? I can't find any other options so I ask here. I hope you can help me. Thanks!
You must set it to the absolute path.
Put this into your bootstrap.php file.
define('PHPWORD_BASE_DIR', realpath(__DIR__));
This is your call:
$domPdfPath = realpath(PHPWORD_BASE_DIR . '/../vendor/dompdf/dompdf');
\PhpOffice\PhpWord\Settings::setPdfRendererPath($domPdfPath);
\PhpOffice\PhpWord\Settings::setPdfRendererName('DomPDF');
//Load temp file
$phpWord = \PhpOffice\PhpWord\IOFactory::load($filepath);
//Save it
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'PDF');
$xmlWriter->save('result.pdf');
I think #Franz Holzingers answer is right on point except that the version of PHP word that I installed today requires the renderer name to be DomPDF. The method fails if it is all caps as shown. Also the version that I installed to day uses a configuration file so while the explicit calls work, if you place them after the configuration file loads, they are overwritten if you place them before the configuration loads. I propose a simpler answer is to locate the file "phpword.ini.dist" located in the directory above "src" in the phpword install directories; edit the line for the DomPDF directory to show where you installed DomPDF and then save the file in the same directory but under the name phpword.ini. Now the configuration loader will perform the calls that Frank documented for you. If you enter the path to DomPDF incorrectly, you get no warning but you can test that it installed correctly using the method \PhpOffice\PhpWord\Settings::getPdfRendererPath(domPdfPath); If the return from that method is empty, it means that the path name you specified in the configuration file did not exist. Note that the loader does not check that the path contains DomPDF, only that the path exists.
It is worth noting that at this time, phpword supports three PDF rendering engines. Any of the three can be configured in the ini file mentioned above by providing the path to the installation and setting the name of the PDF renderer to one of these values (case sensitive): DomPDF, TCPDF or MPDF. DomPDF is recommended and the default but it appears in the code that if you have an investment in or a preference for one of the others, you can used it with phpword.
Related
I'm using mpdf to download an existing file like this-
$mpdf->Output('my_filename.pdf','D');
I need the file downloaded to be read only. Right now the downloaded files can be opened in word and edited, I wish to avoid that.
TL;DR :
I'm downloading an existing file from my system which my clients can edit by opening in word, need to avoid that.I can't have password protection for the files(client requirement)
Use the SetProtection() function as described here.
$filename = 'filename.pdf';
$html = 'Testing PDF protection.';
$mpdf = new mPDF('utf-8', 'A4-P');
$mpdf->SetProtection(array());
$mpdf->WriteHTML($html);
$mpdf->Output($filename,'D');
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:
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
Using phpdocx - I do not have any budget to pay for it and the demo cannot create PDF
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
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?
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');
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);
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.
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();
}
I am attempting to load a .docx file with PHPWord, set a value in it, and save the file. The issue is the saved file comes out as a blank word document, 7kb in size. The document I put in is large and has multiple pages. Obviously its not loading it right. My code is as follows:
$PHPWord = new \PhpOffice\PhpWord\PhpWord();
$document = $PHPWord->loadTemplate('Resources/documents/test.docx');
$document->setValue('theDate', '2014-07-25');
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007');
$xmlWriter->save("php://output");
I have tried with multiple test.docx files of different sorts, but the output file is always blank.
I was able to solve this issue with what Progi1984 linked, calling $document->saveAs() and then reading the file to download it. Thank you :)
I'm trying to create a PDF file, under a Magento phtml file, this is my code :
$pdf = new Zend_Pdf();
$pdf->pages[] = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
$page=$pdf->pages[0]; // this will get reference to the first page.
$style = new Zend_Pdf_Style();
$style->setLineColor(new Zend_Pdf_Color_Rgb(0,0,0));
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES);
$style->setFont($font,12);
$page->setStyle($style);
$page->drawText('example text here',100,($page->getHeight()-100));
$pdf->render();
$pdf->save('test.pdf','true');
My PDF file is created, but I can't open it with acrobat reader.
When I open it with a text editor and compare it with another simple pdf files, I noticed that in the first line was missing in my generated pdf file. it contains "%PDF-1.4"
How can I add this line programmatically with zend_pdf in my pdf file ?
Thanks for help.
According to the zend manual the second save parameter is only for updating files that already exist. In this case you are creating a new file so don't use that option.
$pdf->save('test.pdf');
PS. This answer is technically an RTM statement.
I have a folder with 100s of PDF versions of PPT presentations. I also have a one page PDF file that I want to add to the beginning of each PDF file. Is there a way I can do this with PHP? Could I maybe use the Zend Framework?
It certainly can be done by Zend_Framework!
$pdf = Zend_Pdf::load($fileName);
$frontPdf = Zend_Pdf::load('/path/to/template.pdf');
$frontPage = $frontPdf->pages[0];
//prepend our template front page to PDF
array_unshift($pdf->pages, $frontPage);
//update original document
$pdf->save($fileName, true);
I haven't tested the code here but we have an application working on the same principle.
Check the documentation for pages within Zend_Pdf if you have any problems.