Jump to content

[archived] RockPdf - mPDF helper module


bernhard

Recommended Posts

  • 4 months later...

I'm trying to implement this module at the moment and I also have the problem with the download() and the show() function in my existing site.
save() is working fine.

I tried it in a new installation, too and there it worked fine with all three functions.

I took a look into the generated PDF files and I found the problem. The module adds the HTML Code from the page to the pdf file when using download() and show().
This does not happen with save()
I tried to circumvent this with using the save() and then with this code to start the download:
 

if (file_exists($file)) {
  header('Content-Description: File Transfer');
  header('Content-Type: application/octet-stream');
  header('Content-Disposition: attachment; filename="'.$page->title.'.pdf"');
  header('Expires: 0');
  header('Cache-Control: must-revalidate');
  header('Pragma: public');
  header('Content-Length: ' . filesize($file));
  readfile($file);
  exit;
}


With this code, the HTML code is also added to the PDF file but the HTML is not in the PDF File when it's saved on the server with save(). I don't know how to prohibit that

My solution for now is to implement the code for the PDF download before any oder HTML code is added which works for me.
But If anybody knows how to solve this problem that would be great.

Link to comment
Share on other sites

  • 4 months later...

In case you are looking for a nice and easy replacement for Pages2PDF as you might face problems with it due to PHP 8 - this might help you to get started and helped me today to get a site up and running again. Thanks to @bernhard for this module.

<?php namespace ProcessWire;

// file: templates/invoice.php

$pdf = $modules->get('RockPdf');
$pdf->settings([
    'mode' => 'utf-8',
    'format' => [210, 297],
    'img_dpi' => 300,
    'default_font' => 'DejaVuSans',
]);

// Pseudo header
$pdf->write(wireRenderFile('pages2pdf/_header.php'));

// Footer
$pdf->set('SetHTMLFooter', wireRenderFile('pages2pdf/_footer.php'));

// Body content
$pdf->write(wireRenderFile('pages2pdf/invoice.php'));

// Styleshee
$stylesheet = file_get_contents('pages2pdf/pdf.css');
$pdf->write("<style>$stylesheet</style>");

// Actions
$pdf->show();

In this example I re-use my already existing pages2pdf-templates and even CSS.
It's a super simple PDF, so I really don't have to deal with page margins and such in this case.
So you might want to read the docs of mPDF and RockPDF to get this working in your project.

 

There is a lot more you can do in terms of settings and such which can be found in this post for example:

 

  • Like 4
Link to comment
Share on other sites

On 1/16/2023 at 9:15 PM, wbmnfktr said:

In case you are looking for a nice and easy replacement for Pages2PDF as you might face problems with it due to PHP 8 - this might help you to get started and helped me today to get a site up and running again. Thanks to @bernhard for this module.

<?php namespace ProcessWire;

// file: templates/invoice.php

$pdf = $modules->get('RockPdf');
$pdf->settings([
    'mode' => 'utf-8',
    'format' => [210, 297],
    'img_dpi' => 300,
    'default_font' => 'DejaVuSans',
]);

// Pseudo header
$pdf->write(wireRenderFile('pages2pdf/_header.php'));

// Footer
$pdf->set('SetHTMLFooter', wireRenderFile('pages2pdf/_footer.php'));

// Body content
$pdf->write(wireRenderFile('pages2pdf/invoice.php'));

// Styleshee
$stylesheet = file_get_contents('pages2pdf/pdf.css');
$pdf->write("<style>$stylesheet</style>");

// Actions
$pdf->show();

In this example I re-use my already existing pages2pdf-templates and even CSS.
It's a super simple PDF, so I really don't have to deal with page margins and such in this case.
So you might want to read the docs of mPDF and RockPDF to get this working in your project.

 

There is a lot more you can do in terms of settings and such which can be found in this post for example:

 

I'm already using it and I like it a lot more than pages2pdf.
The download function isn't working for me but I was able to circumvent that problem with a little bit of extra code to create and then to start the download of the pdf.

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

Iam using the following code to generate my PDF in ready.php however after adding the pdf->settings I get a 500 error, Any ideas? 

$pdf = $modules->get('RockPdf');
$pdf->settings([
	'fontdata' => (new \Mpdf\Config\FontVariables())->getDefaults()['fontdata'] + [
	  "far" => [
		'R' => "fa-regular-400.ttf",
		'I' => "fa-regular-400.ttf",
	  ],
	],
  ])

	$pdf->addFont(['Roboto-Regular' => 'site/templates/fonts/Roboto-Regular.ttf',])

	->load("site/templates/test-document.php", ['page' => $pages->get(1053),])
	->save(preview: true);

 

Link to comment
Share on other sites

3 hours ago, Jon said:

Iam using the following code to generate my PDF in ready.php however after adding the pdf->settings I get a 500 error, Any ideas? 

$pdf = $modules->get('RockPdf');
$pdf->settings([
	'fontdata' => (new \Mpdf\Config\FontVariables())->getDefaults()['fontdata'] + [
	  "far" => [
		'R' => "fa-regular-400.ttf",
		'I' => "fa-regular-400.ttf",
	  ],
	],
  ])

	$pdf->addFont(['Roboto-Regular' => 'site/templates/fonts/Roboto-Regular.ttf',])

	->load("site/templates/test-document.php", ['page' => $pages->get(1053),])
	->save(preview: true);

 

EDIT: which version of RockPdf are you using, free or new commercial (https://www.baumrock.com/en/processwire/modules/rockpdf/)?

What is the error message your getting?

Not sure you can invoke a new instance of \Mpdf\Config\FontVariables() and then use the getDefaults() method right away. In the mpdf docs they do it like

$defaultConfig = (new Mpdf\Config\ConfigVariables())->getDefaults();
$fontDirs = $defaultConfig['fontDir'];

$defaultFontConfig = (new Mpdf\Config\FontVariables())->getDefaults();
$fontData = $defaultFontConfig['fontdata'];

$mpdf = new \Mpdf\Mpdf([
    'fontDir' => array_merge($fontDirs, [
        __DIR__ . '/custom/font/directory',
    ]),
    'fontdata' => $fontData + [ // lowercase letters only in font key
        'frutiger' => [
            'R' => 'Frutiger-Normal.ttf',
            'I' => 'FrutigerObl-Normal.ttf',
        ]
    ],
    'default_font' => 'frutiger'
]);

 

  • Like 1
Link to comment
Share on other sites

Thanks gebeer,

I am using the latest pro version. It works ok with the below code however, if I remove the addFont() line, it results in a 500 error. 

$pdf = $modules->get('RockPdf');
$pdf->settings([
	'mode' => 'utf-8',
	'fontdata' => [
	'roboto' => [
          'R' => 'Roboto-Regular.ttf',
          'I' => 'Roboto-Regular.ttf',
        ],
		'robotothin' => [
          'R' => 'Roboto-Thin.ttf',
          'I' => 'Roboto-Thin.ttf',
        ],
	  'far' => [
		'R' => "fa-regular-400.ttf",
		'I' => "fa-regular-400.ttf",
	  ],
	],
	 'default_font' => 'roboto'
  ]);

	$pdf->addFont(['Roboto-Regular' => 'site/templates/fonts/Roboto-Regular.ttf',])
	->load("site/templates/test.php", ['page' => $pages->get(1053),])
	->save(preview: true);
}

 

Link to comment
Share on other sites

9 hours ago, Jon said:

It works ok with the below code however, if I remove the addFont() line, it results in a 500 error. 

What does the error say? Have you turned on debug mode, are you using tracy debugger? Have you lookes in the error logs?

  • Like 1
Link to comment
Share on other sites

10 hours ago, Jon said:

Thanks gebeer,

I am using the latest pro version.

Hey @Jon thx for the purchase and sorry for the trouble. I was confused because you posted your question in the old thread with old code. I'll update this threads title to make it more obvious!

Anyways your code looks still strange. Why are you defining your fonts twice?

Please have a look at the docs (https://www.baumrock.com/en/processwire/modules/rockpdf/docs/fonts/) and use one of the listed fonts exactly like shown in the examples. Once that works try your own font. If it breaks then, share your code and also your font (if possible) so that I can try to reproduce the problem.

Thx

PS: Where is that code that you posted? It has an additional bracket at the end...

  • Thanks 1
Link to comment
Share on other sites

Thanks Both,

I had trouble getting font awesome icons to work using the doc and found your earlier example in this thread. In turn, it seems I have ended up with old and new code. I have resolved the issue by following the docs and font awesome icons are now showing correctly. Code below for reference  

 

$pdf = $modules->get('RockPdf');
$pdf
	->addFont(['R' => 'site/templates/fonts/fa-regular-400.ttf',
			   'I' => 'site/templates/fonts/fa-regular-400.ttf',
			   'B' => 'site/templates/fonts/fa-regular-400.ttf',
			  ], 'far')
	->addFont(['R' => 'site/templates/fonts/Roboto-Regular.ttf',
			   'B' => 'site/templates/fonts/Roboto-Black.ttf',
			  ], 'roboto')
	->load("site/templates/change_management.php")
	->save(preview: true);

 

  • Like 2
Link to comment
Share on other sites

13 hours ago, Jon said:

I had trouble getting font awesome icons to work using the doc and found your earlier example in this thread. In turn, it seems I have ended up with old and new code. I have resolved the issue by following the docs and font awesome icons are now showing correctly. Code below for reference  

Hey @Jon sorry for the trouble, but I have a little present for you ? 

v1.5.0 adds support for directly using fontawesome icon markup like <i class="fa-brands fa-youtube"></i>

icons.png?t=1706365145

The release is less than 10 minutes young and ready for download at https://www.baumrock.com/en/releases/rockpdf/

Docs are already there as well: https://www.baumrock.com/en/processwire/modules/rockpdf/docs/fontawesome/

Hope that helps you and many others ? 

  • Like 3
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...