Jump to content

Generate some PDFs


yesjoar
 Share

Recommended Posts

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

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

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

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

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.

  • Like 1
Link to comment
Share on other sites

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

  • 2 weeks later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...