-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to save Template as PDF #311
Comments
Hi, You need to define your PDF writer. First, download TCPDF or a library as proposed in PhpWord (DomPDF, MPDF...). Define the path of this library : \PhpOffice\PhpWord\Settings::setPdfRendererPath('path/to/tcpdf');
\PhpOffice\PhpWord\Settings::setPdfRendererName('TCPDF'); You need to save your template as docx and then open it with IOFactory and finally save it. $phpWord = new \PhpOffice\PhpWord\PhpWord();
//Open template and save it as docx
$document = $phpWord->loadTemplate('yourtemplate.docx');
$document->saveAs('temp.docx');
//Load temp file
$phpWord = \PhpOffice\PhpWord\IOFactory::load('temp.docx');
//Save it
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'PDF');
$xmlWriter->save('result.pdf'); |
I start the PHP example Sample_07_TemplateCloneRow.php. I have added the following lines at the end of the sample's PHP code.
|
i've problems with pdf writer.. the pdf generated do not contain any images or layout position of source document. |
Hi, cannot see any images either, or page breaks or table cell background colours. $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007'); \PhpOffice\PhpWord\Settings::setPdfRendererPath('tcpdf_min'); Can anyone advise? |
same problem here : cannot see any images either, or page breaks or table cell background colours. |
Me too! Has anybody a solution for this problem? Up |
The answer is no. I've asked half a dozen colleagues and gurus to look into it with me and it just won't work. Ended up producing the whole file again with tcpdf, which sucks cause it cannot look exactly the same. |
Really out of the box thinking is just to let MS Word open the file and save it as a PDF. Hope this helps someone:
|
Right - well that's several hours of my life I'm not going to get back then. Can anybody help me understand this, please? Can i assume that if you are going to go to the trouble of making a word file programatically (and learning to use phpWord in the process) then you are going to want to do something with the word doc you have created. Can i also assume that that is going to be something other than download it and open it in word and say horray i have a word file - otherwise you would have just done it locally in the first place, or written some vba or somehting, right? I am guessing that nearly everybody using phpWord wants to make a pdf from the resulting doc programatically and send it to customers or colleagues or whatever. So what is going on with tcphp. It is just the exact right tool if you want to take the doc that you have made (hours of work) and create a pdf that exactly resembles one that you would have made on the first word processor you bought back in 1988, but otherwise it is not waht you need at all unless you want to program it from scratch. Am i right in believing that you have to tell it how to interpret everything on the page, every font, every color, every image, etc. by setting properties. Phpword is a good piece of work and has clearly been a project requiring many many hours of thought and work, but it has been a waste of time unless there is a simple add on that will create a simple, but loyal pdf. In my case it is a single page invoice, not complicated just a logo some text and a simple link. Has anybody found a solution to this please. In the instructions for phpWord, it says somehting about the html-to-pdf method is used for generating pdfs which is why there is no resident save for that format. Has anybody tried that solution or found a pdfwriter that in any way does a half reasonable job of converting the .docx without too much difficulty. Thanks. [@janssenMA I'm sure you are right about the COM, but i only have a shared server web host at my ISP and i don't think that i can start installing things as i please. I think i just have to use what is there.] |
Understand your frustration. Mind you some people do want to generate a word document by itself without an intention to generate a pdf version of it (it's the case for some of my clients, they want to be able to keep editing the document once generated). 2 solutions (which are both very disappointing compared to 2 lines of code which could have made our lives much, much easier):
|
Hi Underfrog, It sounds as if we have been down the same rocky road. Thank you for the answer, I will look at the link you provided. As i am personally not that bothered about the intermediate step (namely the doc) itself, I am now looking at this. http:https://html2pdf.fr/en/example which might give me a quick route to a pdf that I can attach to an email which is all i really want. I will post again if it works so that others can reduce the pain of learning phpword only to hit a big wall. cheers, Don |
+1 for the images problem. I tried both the PDF and HTML writers. |
+1 any news to this problem?
|
Some more Info, it doesnt load the image from the Office-ZIP correctly:
|
So we don't have a good converter yet, right? I'm generating a template but I would like to have images in the pdf. If anybody find something, write here. Thank you. |
@jslirola : I use unoconv (https://github.com/dagwieers/unoconv) it's not a perfect solution, it's not PHP. But it's work. 1- use a normal word template (phpoffice word template) |
Thank you @gplaza, I saw that solution but it require unoconv installed on the server. I checked another options without luck:
|
Hi All, In My case docx is converted in pdf with DomPDF but header and footer disappear in pdf. any idea to get header and footer in pdf file? |
I'm using PHPWord, thanks to share this project, for applying templates. I load a doc or odt and save it. And for exporting pdf, I'm using libreoffice directly by command line as php can run exec. The command is easy Sure it would be nice to use only phpword, but libreoffice is stable and most of the other projects use it for export pdf. |
Thanks - this discussion was quite helpful. |
This is a large drawback of using PHPWord: my application needs to generate word/odf versions and PDF versions ... Strange the developers did not think this through |
@eothein If you are with time constraints I encourage you to use a paid version that are being offered with such functionality. I never used them, so I do not which one to recommend, but there are multiple out there. The smear you make is quite hurtful. 'not think this through'... If you are dissatisfied, I'll give you your money back. Open source projects thrive with a mix of user-programmers that are willing to donate some time to make the product better. The core idea, Writing a docx from scratch works very well. I like this Unix idea of doing one thing well. The rest, like the TemplateProcessor was added in by others, and support for other formats was started, but not finished However, Stick around, and with your help, the PDF exporter will get better. Just keep in mind this PHPWord is not 1.0 yet. And what is the drawback of using more than one technology? Just use https://github.com/dagwieers/unoconv side by side with PHPWord. Let each do what they are good at. Note: if you are using oowriter directly from the commandline... it is heavy (java) and takes a few seconds. Other options: Use PostScript (GhostWriter) to produce perfect pdf's |
Dear FBnil I changed my comment a little bit (LARGE to large), it was in on way my intention to be hurtful. Well let me explain my frustration: when I was choosing a framework to generate odf/pdf versions of a document, I came across PHPWord which is encouraged on a lot of forums. On the documentation site I found in one of the examples:
So my guess was that the functionality was already there. So trying and retrying to find out that after several hours the functionality is not complete was a frustrating. Maybe this explains my word choosing. My sincere apologies. Jens Buysse |
@eothein : Yes, I can see why you feel it wasted your time, but you now know it's weaknesses, but do not forget it's strengths. May I kindly re-suggest, the UNO interface from OpenOffice?
There are many UNO bindings for many languages that relaunch the OpenOffice daemon and allow simple parameters for conversions. This dramatically saves time for high usage conversions. |
@nicolasidweb is it possible to |
Googling it is possible: |
i have issue with multiple languages convert docx file to pdf file
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
Wow ! It works so good !!! I have a word file with logo, images, text. I tested all (tcpdf, fpdf, libreoffice, mpdf, dompdf) and it's the only one who works perfectly, like a pdf with word directly. |
I've created an ivoice by using a Template.
How can I save this to PDF?
When using \PhpOffice\PhpWord\IOFactory::createWriter($doc, 'PDF');
It says $doc is not an instance of PHPWord. How do I get back the PHPWord object?
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
The text was updated successfully, but these errors were encountered: