Jump to content

RockPdf - mPDF helper module


Recommended Posts

@dotnetic  Yes as it looks, the browser seems to render the page correctly (I can't find any errors at least in "tracy" and in Chrome "DevTools").

My error that comes looks like this:

(first one = Chrome  // second one = Edge)

Chrome.png.c62910f2ff5bf1e157ec2ad8f9586e00.png       Edge.png.869d2df667eb6ec1485c9b78d44bb98e.png

Link to comment
Share on other sites

I've had this error sometimes and it was due to a broken pdf (don't know the reason). I don't understand why you want to use markup regions for rendering HTML of the pdf?? Are you trying to render a regular page HTML as PDF?? I don't think that's a good idea. In a regular HTML page you have lots of code that mPDF will at least not understand and in the worst case will break your pdf. So I'd recommend throwing custom HTML to mPDF instead of regular page markup. You'll have a lot more control over your pdf and a lot less troubles.

Link to comment
Share on other sites

2 hours ago, bernhard said:

I don't understand why you want to use markup regions for rendering HTML of the pdf??

you misunderstood me then, i don't want to use markup regions to render html.
I just thought that because I use the template "Regular" and this is based on markup regions, it is probably because of that? Because the same code works for another project where I have chosen a direct output. See in this thread on page 2.

I will shown below my actual structure....


2 hours ago, bernhard said:

Are you trying to render a regular page HTML as PDF??

actual in my output-pdf.php File I have only this code

$pdf = $modules->get('RockPdf');
$pdf->set('SetHeader', 'This is my header text');
$pdf->write('Hello World ' . date('H:i:s'));
$pdf->write('<!-- my custom comment -->');
// Output
$pdf->show(); // to directly show your file in the browser

so I think if I understood the question correctly, the answer is no?


ok maybe it's easier if i just post my code to show what i'm trying to do




(with activated URL segments in the backend to the "management.php" template)

// Login protection for this page by LoginRegisterPro
if($user->isLoggedin() != true) $session->redirect($config->urls->root.'user/', false);

// Checking the authorization to display the administration page
if($user->hasRole("editor") != true) $session->redirect($config->urls->root, false);

if(strlen($input->urlSegment2)) throw new Wire404Exception();

switch($input->urlSegment1) {
	case '':

		// Output to the content-body ?>
		<div id='content-body'>

			<? // The following code will later control the content that will appear on the PDF ?>

			Please select an entry:<br>
			<form class="uk-form-stacked" action="<?=$page->url;?>pdf/" target='_new' method="post">
				<input type="hidden" name="check" value="PDFdruck">
				<div class="uk-margin">
					<div class="uk-form-controls">
						<select class="uk-select" id="form-stacked-select" name="assignment"><?
							foreach($pages->find("template=lesson, date_from<=today, sort=-date_from, sort=-date_time") as $lessonID){
								echo"<option value='". $lessonID->id ."'>". $lessonID->date_from ." ". $lessonID->date_time ." clock</option>";
							} ?>
				<input type="submit" class="uk-button uk-button-secondary uk-button-small" value="Output status as PDF">

		<!-- content-body -->



	case 'pdf':
		// Display PDF in Browser
		include 'pdf.php';


		// Anything else? Throw a 404
		throw new Wire404Exception();



actual for testing (but even that unfortunately does not work...)


 * Outputs a PDF for independent printing
 * Version 2021-02-xx

// Code for the creation of a PDF
$pdf = $modules->get('RockPdf');
$pdf->set('SetHeader', 'This is my header text');

$pdf->write('Hello World ' . date('H:i:s'));
$pdf->write('<!-- my custom comment -->');

// Output
$pdf->show(); // to directly show your file in the browser


And that's it, with these specifications come the errors from above....



Link to comment
Share on other sites

I'd suggest the following:

First, disable markup regions to confirm that it works when markup regions are disabled. Then enable markup regions and to this:

// replace this
include 'pdf.php';

// by that:
echo "include PDF here";

Then load your page that should create the PDF and inspect the HTML (right click -> show sourcecode). What does the source code look like?

Link to comment
Share on other sites

  • 2 weeks later...
On 2/21/2021 at 12:14 PM, dotnetic said:

Your example pdf.php looks absolutely fine. Please try to save the PDF file via $pdf->save() or $pdf->download() and then take a look if the PDF is generated.


I have tried and the result is that 
$pdf->show() => does not work
$pdf->download() => does not work
$pdf->save() => works, puts the correctly generated PDF on the server

so it seems to be rather a problem with the output method "markup regions" which I use, respectively that I still have a wrong code structure here or?


On 2/22/2021 at 8:53 AM, bernhard said:

I'd suggest the following:

First, disable markup regions to confirm that it works when markup regions are disabled. Then enable markup regions and to this:

// replace this
include 'pdf.php';

// by that:
echo "include PDF here";

Then load your page that should create the PDF and inspect the HTML (right click -> show sourcecode). What does the source code look like?

Thanks bernhard, I went through the points one by one
1. I deactivate markup regions in config.php => output in browser works now! (However, the design no longer fits)

2a. I enable markup regions again and test the change from above (I'm not quite sure now if I should really just put the "echo" in the "case 'pdf': "? I do it, and it outputs my design (up to the header, then a white body, and the footer again at the bottom). The only thing it outputs to me at the bottom of the pw-region-debug line is "No regions" and the time of generation, nothing else.

2b. I interpret your echo to mean that I should paste the code from my pdf.php file here instead of the include command. Unfortunately the result is again the same as above with dotnetic's suggestion. The only code that works to create a PDF is save().

3. "... what does the source code look like?" => Unfortunately there is no real HTML code to look at or parse (except for the one in point 2a, but there it doesn't process anything....) so I can't share anything.

Sorry if I'm a bit clumsy here.... I am therefore very grateful for any help.

Link to comment
Share on other sites

  • 1 month later...

Hi @bernhard "rockpdf" works just fine for my needs but I still have two little problems:

1. I already have a little form with takes a few inputs (text values) which get nicely saved too when the pdf is generated and saved with "rockpdf" on the server sending me an email. I only do not know how to attach the output.pdf before submitting the email form. May you can help?

2. Not so important right know for me but I also realized "$pdf->show() => does not work"  like @Ralf mentioned just above does not work for me either on a very minimal example.

Link to comment
Share on other sites

Thanks @bernhard for the link but there is still a problem for me, because:

1. If use the examples of the provided link it works fine for sending an email with $m->... but I do not know how to save the pdf this way and therefore no pdf-file is attached

  If I use something like $pdf->save() I get a failure which tells me "... not allowed in this context ..."

2. Could may provided just very basic example code how that

$pdf->... in your module works together with $m->... you provided in the link above?

All I need would be: A pdf created by RockPdf which gets send as an attachment by $m->... (the link you provided ... )



Link to comment
Share on other sites

It's all in the docs:


Then simply use the path of the pdf as path of the attachment:

$file = $pdf->save();

$mail = new WireMail();


Link to comment
Share on other sites

  • 1 month later...

Hi to everybody!

I found this useful module and it works fine on desktop (either pc and mac, Chrome/Safari/Opera/FireFox/Vivaldi and IE), but sometimes I've encountered different behaviours on mobile (tested on Android and iOs, with Chrome, Firefox, Opera, and Safari).

Briefly, I built an html table in delayed output (a race ranking, one for each different race pages), I collect it into a variable and passed it throug a $_SESSION index to another page where it's grabbed with a ob_start()/ob_get_contents()/ob_end_clean() statement (no echos are present out of this one ob_start/ob_end).

Despite the desktop pdf output it's ok (I can show()/save()/download() the files and open them with Acrobat, and into docs properties I found each custom setting, e.g. title, author, application and so on) on mobile, instead, I encountered two kind of issues:

1) Most of  times, in Chrome, I was alerted of a non valid file, or FireFox it's not able to save pdf file.
2) Some times the file is outputted also on mobile and it's perfectly readble, but on save and on open by Acrobat App I found the content empty with the notice of the $_SESSION['nameofindex'] undefined.

N.B. Same results also calling directly mPdf after RockPdf and setting it's proper methods.
No problems with a "static" html code passed to the write() method, but I'm getting crazy figuring why it works fine on desktop and not on mobile (or it's ok also on mobile, but can't save a correct file) with $_SESSION.

Does anyone encontered same kinds of these strange behaviours?

Any suggestion about that?


Many thanks in advance.


Link to comment
Share on other sites

  • 3 weeks later...
On 9/14/2020 at 9:28 AM, bernhard said:

Thx @axelino and welcome to the forum 🙂 

I tried to test the library quickly but could not make it work. Could you please provide a PR and usage instructions for the readme so that I can check it quickly? Then I'm happy to add it to the module 🙂 

Hello @bernhard, sorry it took a little longer but I just submitted the Pull-Request.
Please take a look at it and let me know if further adjustments or info is needed.

Thank you! 🙂

Link to comment
Share on other sites

  • 4 weeks later...
1 hour ago, tires said:

I just want to have a simple "download this page as pdf" button on my page.

Why not just use the browser features with @media print in the css?

Link to comment
Share on other sites

13 minutes ago, MarkE said:

Why not just use the browser features with @media print in the css?

I am still using a css with "@media print".
What browser feature do you mean?

Link to comment
Share on other sites

  • 4 weeks later...
On 7/21/2021 at 12:18 AM, tires said:

I want to offer a download function to the website visitors ... not only for myself.

Is there really no way to get a simple "download as pdf" button?

Link to comment
Share on other sites

Hi @tires

Of course you can do that! See this example:

Place this in site/ready.php (with a current dev version of PW supporting url hooks - otherwise you need to create a custom endpoint for generating the pdf. Or you could use url segments and an if-statement as well):

$wire->addHook("/pdf", function($event) {
  $pdf = $event->modules->get('RockPdf');
  $pdf->write('Hello World');

Then add a button to your HTML template file:

<a href="/pdf">Your button/link label</a>

The content and rendering of the PDF is totally up to you.

  • Like 1
Link to comment
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.
  • Similar Content

    • By monollonom
      (once again I was surprised to see a work of mine pop up in the newsletter, this time without even listing the module on PW modules website 😅. Thx @teppo !)
      Github: https://github.com/romaincazier/FieldtypeQRCode
      Modules directory: https://processwire.com/modules/fieldtype-qrcode/
      A simple fieldtype generating a QR Code from the public URL of the page, and more.
      Using the PHP library QR Code Generator by Kazuhiko Arase.

      In the field’s Details tab you can change between .gif or .svg formats. If you select .svg you will have the option to directly output the markup instead of a base64 image. SVG is the default.
      You can also change what is used to generate the QR code and even have several sources. The accepted sources (separated by a comma) are: httpUrl, editUrl, or the name of any text/URL/file/image field.
      If LanguageSupport is installed the compatible sources (httpUrl, text field, ...) will return as many QR codes as there are languages. Note however that when outputting on the front-end, only the languages visible to the user will be generated.
      Unformatted value
      When using $page->getUnformatted("qrcode_field") it returns an array with the following structure:
      [ [ "label" => string, // label used in the admin "qr" => string, // the qrcode image "source" => string, // the source, as defined in the configuration "text" => string // and the text used to generate the qrcode ], ... ] Formatted value
      The formatted value is an <img>/<svg> (or several right next to each other). There is no other markup.
      Should you need the same markup as in the admin you could use:
      $field = $fields->get("qrcode_field"); $field->type->markupValue($page, $field, $page->getUnformatted("qrcode_field")); But it’s a bit cumbersome, plus you need to import the FieldtypeQRCode's css/js. Best is to make your own markup using the unformatted value.
      Static QR code generator
      You can call FieldtypeQRCode::generateQRCode to generate any QR code you want. Its arguments are:
      string $text bool $svg Generate the QR code as svg instead of gif ? (default=true) bool $markup If svg, output its markup instead of a base64 ? (default=false) Hooks
      Please have a look at the source code for more details about the hookable functions.
      $wire->addHookAfter("FieldtypeQRCode::getQRText", function($event) { $page = $event->arguments("page"); $event->return = $page->title; // or could be: $event->return = "Your custom text"; }) $wire->addHookAfter("FieldtypeQRCode::generateQRCodes", function($event) { $qrcodes = $event->return; // keep everything except the QR codes generated from editUrl foreach($qrcodes as $key => &$qrcode) { if($qrcode["source"] === "editUrl") { unset($qrcodes[$key]); } } unset($qrcode); $event->return = $qrcodes; })
    • By Sebi
      AppApiFile adds the /file endpoint to the AppApi routes definition. Makes it possible to query files via the api. 
      This module relies on the base module AppApi, which must be installed before AppApiFile can do its work.
      You can access all files that are uploaded at any ProcessWire page. Call api/file/route/in/pagetree?file=test.jpg to access a page via its route in the page tree. Alternatively you can call api/file/4242?file=test.jpg (e.g.,) to access a page by its id. The module will make sure that the page is accessible by the active user.
      The GET-param "file" defines the basename of the file which you want to get.
      The following GET-params (optional) can be used to manipulate an image:
      width height maxwidth maxheight cropX cropY Use GET-Param format=base64 to receive the file in base64 format.
    • By MarkE
      This fieldtype and inputfield bundle was built for storing measurement values within a field, rendering them in a variety of formats and converting them to other units or otherwise modifying them via the API.
      The API consists of a number of predefined functions, some of which include...
      render() for rendering the measurement object, valueAs() for converting the value to another unit value, convertTo() for converting the whole measurement object to different units, and add() and subtract() for for modifying the stored value by the value (converted as required) in another measurement. In the admin the inputfield includes a checkbox (which can be optionally disabled) for converting values on page save. For an example if a value was typed in as centimeters, the unit was changed to metres, and the page saved with this checkbox selected, said value would be automatically converted so that e.g. 170 cm becomes 1.7 m.

      A simple length field using Fieldtype Measurement and Inputfield Measurement.
      Combination units (e.g. feet and inches) are also supported.
      Please note that this module is 'proof of concept' at the moment - there are limited units available and quite a lot of code tidying to do. More units will be added shortly.
      See the GitHub at https://github.com/MetaTunes/FieldtypeMeasurement for full details and updates.
    • By tcnet
      File Manager for ProcessWire is a module to manager files and folders from the CMS backend. It supports creating, deleting, renaming, packing, unpacking, uploading, downloading and editing of files and folders. The integrated code editor ACE supports highlighting of all common programming languages.

      This module is probably the most powerful module. You might destroy your processwire installation if you don't exactly know what you doing. Be careful and use it at your own risk!
      ACE code editor
      This module uses ACE code editor available from: https://github.com/ajaxorg/ace

      This module uses the JavaScript dragscroll available from: http://github.com/asvd/dragscroll. Dragscroll adds the ability to drag the table horizontally with the mouse pointer.
      PHP File Manager
      This module uses a modified version of PHP File Manager available from: https://github.com/alexantr/filemanager
    • By tcnet
      This module implements the website live chat service from tawk.to. Actually the module doesn't have to do much. It just need to inserted a few lines of JavaScript just before the closing body tag </body> on each side. However, the module offers additional options to display the widget only on certain pages.
      Create an account
      Visit https://www.tawk.to and create an account. It's free! At some point you will reach a page where you can copy the required JavaScript-code.

      Open the module settings and paste the JavaScript-code into the field as shown below. Click "Submit" and that's all.

      Open the module settings
      The settings for this module are located int the menu Modules=>Configure=>LiveChatTawkTo.

  • Create New...