Jump to content

[archived] RockPdf - mPDF helper module


bernhard

Recommended Posts

Attention: This is the thread for the archived open source version of RockPdf. This module is neither developed any further nor maintained nor will it get any fixes.

Modules Directory: https://modules.processwire.com/modules/rock-pdf/
Download & Docs: https://github.com/BernhardBaumrock/RockPDF

Please see the new version here:

 

  • Like 13
  • Thanks 4
Link to comment
Share on other sites

  • 2 weeks later...

Thanks Benhard for this module, small modification i've updated the save() function to allow various output types as stated:

https://mpdf.github.io/reference/mpdf-functions/output.html

 

/* ########## output ########## */

  /**
   * save output to:
    local file ($dest = "FILE")
    download ($dest = "DOWNLOAD")
    inline to browser (default - $dest = "INLINE")
   */
    
  public function save($filename = 'output.pdf', $dest = "INLINE") {
    if(!$filename) $filename = 'output.pdf';
    if($dest == "FILE") $filename = $this->getAbsolute($filename);  // save to disk
    $this->mpdf->Output($filename, constant('\Mpdf\Output\Destination::'.$dest));
  }

// stream to browser
$pdf->save();
$pdf->save('filename.pdf')
$pdf->save('filename.pdf,'INLINE);

// force download
$pdf->save('myfile.pdf','DOWNLOAD'); 

// save locally
$pdf->save('myfileserver.pdf','FILE'); 

 

  • Like 3
Link to comment
Share on other sites

  • 1 month later...
  • 3 weeks later...

bernhard,

I've been working on a project with mPDF and there are lots of variants to the PDF content and how they are saved/downloaded, etc...

It got me thinking about making a PW helper module to make things a little more sane to deal with. I decided to see how others were dealing with PDF creation within ProcessWire, and whaddya know — what I was imagining already exists.

Just wanted to say thank you!

  • Like 5
Link to comment
Share on other sites

  • 5 weeks later...
  • 3 weeks later...

I have problems printing images embedded into a textarea if the config.php has this setting: $config->pagefileSecure=true

ERROR: Could not find image file (.............. /site/assets/files/2922/unterschrift.jpg)

The file actually is stored under this path: ............... /site/assets/files/-2922/unterschrift.jpg

Link to comment
Share on other sites

Did anyone else encounter problems with `show()` and `download()`? If I use them, all I get is blank pages resulting in an invalid PDF. The `save()` function works as expected though.

I’m running PW 3.0.98 with PHP 7.1.12 and RockPdf 1.0.1

Link to comment
Share on other sites

Latest Chrome/Safari/Firefox on macOS 10.14 show all the same problem. The weird thing is it creates the correct amount of pages in the PDF, they’re just blank.

Calling `$mpdf->Output($filename, \Mpdf\Output\Destination::DOWNLOAD);` directly results in the same error.

  • Thanks 1
Link to comment
Share on other sites

What kind of mime-type errors? And how could that be related to PW only but not the webserver or PHP in general?

I also just checked it on the webserver (was on localhost before): same problem there. It’s weird that one option would work while the other two wouldn’t. I can’t make sense of it atm.

Link to comment
Share on other sites

Quote

JqueryCore.js?v=1539002242:2 Resource interpreted as Document but transferred with MIME type application/pdf: "http://www.rockcrm.test/site/assets/files/1156/hn2018-029.pdf?modal=panel&pw_panel=1".

PW can set the mime type, so I guess it could be related to it. But I've never used this feature, so I don't really have an idea. I also don't have time to look into that atm. Maybe someone is more experienced in this area and can help...

Edit-Template--basic-page-•.png

Link to comment
Share on other sites

Just a note if anybody gets in trouble when creating complex PDFs... It seems that there is some bug related to the tempdir that is created for the pdf. If the PDF takes long to render it can happen that this folder is deleted automatically before the PDF is done. Then you'll get an error. In my case this solved it:

$pdf = $modules->get('RockPdf');
$pdf->settings([
  'tempDir' => $this->config->paths->root . 'site/assets/cache/WireTempDir/.RockPdf/test/',
]);

For sure just a quickfix, but I'm busy...

  • Like 2
Link to comment
Share on other sites

22 hours ago, bernhard said:

Just a note if anybody gets in trouble when creating complex PDFs... It seems that there is some bug related to the tempdir that is created for the pdf. If the PDF takes long to render it can happen that this folder is deleted automatically before the PDF is done. Then you'll get an error. In my case this solved it:


$pdf = $modules->get('RockPdf');
$pdf->settings([
  'tempDir' => $this->config->paths->root . 'site/assets/cache/WireTempDir/.RockPdf/test/',
]);

For sure just a quickfix, but I'm busy...

Hmm, that didn’t help on my side. I still see the "Unable to remove" errors in the wire-temp-dir logfile, now they’re just referring to the newly set directory. And yes, these are more complex PDFs, but not overly complex (~3MB).

Link to comment
Share on other sites

22 hours ago, bernhard said:

If the PDF takes long to render it can happen that this folder is deleted automatically before the PDF is done.

FYI, you can set the age the temp directory lives for with the maxAge option:

$files->tempDir('RockPdf', ['maxAge' => 600]);
// or
$files->tempDir('RockPdf', 600);

 

  • Like 1
Link to comment
Share on other sites

23 hours ago, bernhard said:

PW can set the mime type, so I guess it could be related to it. But I've never used this feature, so I don't really have an idea.

 

This will not work for me as I generate the PDF if there’s a "pdf" GET parameter found (this is e.g. how https://github.com/wanze/Pages2Pdf handles it as well). That’s why I was pursuing another way meanwhile: the wireSendFile() function (https://github.com/processwire/processwire/blob/master/wire/core/Functions.php#L257https://github.com/wanze/Pages2Pdf/blob/master/Pages2Pdf.module#L312).

Here’s the relevant code from my template file:

$inputPdf = $input->get('pdf');
$inputPdfSan = $sanitizer->int($inputPdf);

if ($inputPdfSan == 1) {
    $pdf = $modules->get('RockPdf');
    $pdf->settings([
        'tempDir' => $this->config->paths->root . 'site/assets/cache/WireTempDir/.RockPdf/test/',
        ],
        'mode' => 's',
        'mirrorMargins' => 1,
    ]);
    $mpdf = $pdf->mpdf;

	$mpdf->WriteHTML('Hello World ' . date('H:i:s'));
	$filename = $page->name . '-pdf-' . $page->id . '.pdf';
	$response = $pdf->save($filename);
    wireSendFile($response->path, array('forceDownload' => true));
}

However, the problem remains: the PDFs do get saved correctly, but when trying to download them (either via wireSendFile() or RockPDF’s download()), they are blank or corrupted.

Link to comment
Share on other sites

20 minutes ago, Robin S said:

FYI, you can set the age the temp directory lives for with the maxAge option:


$files->tempDir('RockPdf', ['maxAge' => 600]);
// or
$files->tempDir('RockPdf', 600);

  

Thanks for the hint, wasn’t aware of that. But if the default age for a temp folder is 120 seconds, then that should be plenty of time already. The PDF generation maybe takes around 3 seconds.

Link to comment
Share on other sites

52 minutes ago, Robin S said:

YI, you can set the age the temp directory lives for with the maxAge option:

Thx robin I know about that but somehow it didn't work. I need to see why and when that happens, but setting the maxage didn't solve it unfortunately.

Link to comment
Share on other sites

  • bernhard changed the title to [archived] RockPdf - mPDF helper module

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
×
×
  • Create New...