Jump to content
bernhard

mpdf storing pdf to file system

Recommended Posts

hi guys,

i'm playing around with mpdf library (of course i know pages2pdf module) :)

$mpdf = new mPDF();

$mpdf->WriteHTML('<style>' . wireRenderFile('styles/invoice.css') . '</style>');
$mpdf->WriteHTML(wireRenderFile('markup/invoice.php'));

$mpdf->Output('invoice.pdf', 'F');
exit;

this creates "invoice.pdf" at /site/templates

i want it to be saved at /site/assets/files/pageid so i tried

$mpdf->Output('../assets/invoice.pdf', 'F');

wich worked, but

$mpdf->Output($config->urls->assets . 'invoice.pdf', 'F');

didn't!

Warning: fopen(/site/assets/invoice.pdf): failed to open stream: No such file or directory in /var/www/html/billing.dev/site/modules/mpdf60/mpdf.php on line 8335
mPDF error: Unable to create output file: /site/assets/invoice.pdf

why does the first work and the second does not?

thanks for your time! :)

Share this post


Link to post
Share on other sites

I guess you need to pass path and no URL, try $config->paths.

  • Like 2

Share this post


Link to post
Share on other sites

Actually if you want it to save to the assets/files/pageid folder for the current page, this is what you want:

$page->filesManager()->path();

But do you want to save it directly, or would you rather just add it to a files field for the page?

$page->pdfs->add("invoice.pdf");

If you went that route you could do:

$mpdf->Output('/tmp/invoice.pdf', 'F');
$page->pdfs->add("/tmp/invoice.pdf");
unlink("/tmp/invoice.pdf");

or something like that :)

  • Like 2

Share this post


Link to post
Share on other sites

thank you tpr and adrian,

@adrian - that was what i wanted to achieve but there is some strange behaviour :(

$filename = "invoice.pdf"; 

$mpdf->Output($filename, 'F'); // creates file /site/templates/invoice.pdf correctly
$mpdf->Output($filename, 'I'); // shows the file in the browser

ChromePhp::log(file_exists($filename)); // returns true

$page->of(false);
$page->invoice_pdfs->add($filename); // creates a 0 byte file entry in the field, but no file in the page's files folder
$page->invoice_pdfs->add("invoice.pdf"); // same as above
$page->invoice_pdfs->add("/site/templates/" . $filename); // does nothing and seems to break (no "added file..." log)
$page->save();

ChromePhp::log('added file ' . $filename);

error when editing the page with the 0-byte file in the file-field:

Warning: filemtime(): stat failed for /var/www/html/billing.dev/site/assets/files/1567/invoice.pdf in /var/www/html/billing.dev/wire/core/Pagefile.php on line 324

thanks for your help!! :(

Share this post


Link to post
Share on other sites

ok it works now - you were both right (as expected) :) it was important to use the PATH to the file (thanks tpr, $config->paths->templates)

$filename = $config->paths->templates . $page->parent->invoice_type . '_' . $page->invoice_num . '_' . $page->invoice_to->client_company . '_' . $page->invoice_to->client_forename . '_' . $page->invoice_to->client_surname . '.pdf';
$mpdf->Output($filename, 'F');

$page->of(false);
$page->invoice_pdfs->add($filename);
$page->save();
unlink($filename);

$session->redirect($page->invoice_pdfs->last()->url);

one more question: 

i was not able to save the pdf to the assets folder, so i guess this is because of some security issues? how would i create files in a different folder above the current working directory? just curious :)

Share this post


Link to post
Share on other sites

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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...