yesjoar Posted February 21, 2013 Share Posted February 21, 2013 Hey there, for a new project I need some tricky stuff. The customer need a pdf from his content elements. My idea was to build a module, which create a pdf each time the page will be saved. At the moment I have a init with addHookAfter('save', $this, 'writepdf); In the function writepdf I want to create the pdf and then save this in a file-field, which the page has. Is this the correct way or would you do this on a other way? I don't want to create the pdf every time a user clicked the pdf-link in the frontend, so my idea was to do this on this way? Would be nice to hear from you with a little input. (: Link to comment Share on other sites More sharing options...
Wanze Posted February 21, 2013 Share Posted February 21, 2013 Hi yesjoar, This sounds like a good idea. You could also check if there were made some modifications to the content fields before creating a new pdf. For creating the pdf's, I can recommend tcpdf. Used it a few times and it has UTF-8 and basic CSS support. Cheers Link to comment Share on other sites More sharing options...
yesjoar Posted February 21, 2013 Author Share Posted February 21, 2013 Thanks for your post. Yes, the idea sounds good and simple, but I have many problems with that. $this->pages->addHookAfter('save', $this, 'writepdf'); So, this is my Hook. So the function writepdf will be called AFTER the page will be saved. Now I get the elements which will be in my pdf document, but how I can save the generated document now? I mean, the page is saved already… Know what I mean? So I've no idea what's the right approach is… Link to comment Share on other sites More sharing options...
Wanze Posted February 21, 2013 Share Posted February 21, 2013 This would be a modification with the API. So inside your 'writepdf' function, do something like this (this example uses the tcpdf class): require_once('PATH_TO_YOUR_TCPDF_CLASS/tcpdf.php'); $page = $event->arguments[0]; // create new PDF document - taken from an example on tcpdf.org $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->AddPage(); $html = $page->render(); //Instead of the method below you maybe want to create a "PDF-Template" for your site $pdf->writeHTML($html, true, false, true, false, ''); $filename = $this->config->paths->assets . 'pdf_tmp/' . $page->name . '.pdf'; $pdf->Output($filename, 'F'); //Save the pdf to temp folder //Add the pdf to the page $page->of(false); $page->files->add($filename); $page->save('files'); //Remove temp pdf unlink($filename); I don't think it will work, but something like this should. Does it help? Link to comment Share on other sites More sharing options...
teppo Posted February 21, 2013 Share Posted February 21, 2013 Minor addition: in cases like this you can limit save to a single field with $page->save('files'). Also, I'm not really sure about this and can't test right now, but if you're doing what Wanze explained above (hooking into pages->save() and running pages->save() there..) you could end up with a loop. By saving only files field at hook function you should be able to avoid that. 1 Link to comment Share on other sites More sharing options...
Wanze Posted February 21, 2013 Share Posted February 21, 2013 Ups Teppo's probably right I didn't think of that loop. Please try with $page->save('files'). Cannot modify my post im on mobile. Another approach would be to create the pdf on first click and then save it. The next clicks can load the saved pdf (cache). Then you just have to delete the pdf on page save to make sure it gets recreated again on the next click. Link to comment Share on other sites More sharing options...
Soma Posted February 21, 2013 Share Posted February 21, 2013 Or you can save a temp value zo the page when saving like $page->skip = 1 and check for that before in the beginning of the hook. Link to comment Share on other sites More sharing options...
Wanze Posted February 22, 2013 Share Posted February 22, 2013 Coming soon: Pages2Pdf module 5 Link to comment Share on other sites More sharing options...
Soma Posted March 9, 2013 Share Posted March 9, 2013 Coming soon: Pages2Pdf module You mean like this? http://modules.processwire.com/modules/pdfcomposer/ Link to comment Share on other sites More sharing options...
apeisa Posted March 9, 2013 Share Posted March 9, 2013 More like this:http://processwire.com/talk/topic/3008-module-pages2pdf/#entry29627 Much more usable approach in my opinion. Link to comment Share on other sites More sharing options...
Wanze Posted March 10, 2013 Share Posted March 10, 2013 You mean like this? http://modules.processwire.com/modules/pdfcomposer/ I didn't realize that there was already a pdf module. Looks like Pdf Composer is a Process-module which generates pdfs of one or more pages, while Pages2Pdf is meant to be used in the frontend. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now