Jump to content

RockPdf - mPDF helper module


bernhard

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


 

Spoiler

 

management.php
(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>";
							} ?>
						</select>
					</div>
				</div>
				<input type="submit" class="uk-button uk-button-secondary uk-button-small" value="Output status as PDF">
			</form>

		</div>
		<!-- content-body -->

		<?

	break;

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

	break;

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

?>

 

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

pdf.php


<?
/**
 * 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.

@dotnetic

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:

save.png

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

$file = $pdf->save();

$mail = new WireMail();
...
$mail->attachment($file->path);
$mail->send();

 

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');
  $pdf->download("download-button.pdf");
});

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 Robin S
      This module lets you add some custom menu items to the main admin menu, and you can set the dropdown links dynamically in a hook if needed.
      Sidenote: the module config uses some repeatable/sortable rows for the child link settings, similar to the ProFields Table interface. The data gets saved as JSON in a hidden textarea field. Might be interesting to other module developers?
      Custom Admin Menus
      Adds up to three custom menu items with optional dropdowns to the main admin menu.
      The menu items can link to admin pages, front-end pages, or pages on external websites.
      The links can be set to open in a new browser tab, and child links in the dropdown can be given an icon.
      Requires ProcessWire v3.0.178 or newer.
      Screenshots
      Example of menu items

      Module config for the menus

      Link list shown when parent menu item is not given a URL

      Advanced
      Setting child menu items dynamically
      If needed you can set the child menu items dynamically using a hook.
      Example:
      $wire->addHookAfter('CustomAdminMenus::getMenuChildren', function(HookEvent $event) { // The menu number is the first argument $menu_number = $event->arguments(0); if($menu_number === 1) { $colours = $event->wire()->pages->findRaw('template=colour', ['title', 'url', 'page_icon']); $children = []; foreach($colours as $colour) { // Each child item should be an array with the following keys $children[] = [ 'icon' => $colour['page_icon'], 'label' => $colour['title'], 'url' => $colour['url'], 'newtab' => false, ]; } $event->return = $children; } }); Create multiple levels of flyout menus
      It's also possible to create multiple levels of flyout submenus using a hook.

      For each level a submenu can be defined in a "children" item. Example:
      $wire->addHookAfter('CustomAdminMenus::getMenuChildren', function(HookEvent $event) { // The menu number is the first argument $menu_number = $event->arguments(0); if($menu_number === 1) { $children = [ [ 'icon' => 'adjust', 'label' => 'One', 'url' => '/one/', 'newtab' => false, ], [ 'icon' => 'anchor', 'label' => 'Two', 'url' => '/two/', 'newtab' => false, 'children' => [ [ 'icon' => 'child', 'label' => 'Red', 'url' => '/red/', 'newtab' => false, ], [ 'icon' => 'bullhorn', 'label' => 'Green', 'url' => '/green/', 'newtab' => false, 'children' => [ [ 'icon' => 'wifi', 'label' => 'Small', 'url' => '/small/', 'newtab' => true, ], [ 'icon' => 'codepen', 'label' => 'Medium', 'url' => '/medium/', 'newtab' => false, ], [ 'icon' => 'cogs', 'label' => 'Large', 'url' => '/large/', 'newtab' => false, ], ] ], [ 'icon' => 'futbol-o', 'label' => 'Blue', 'url' => '/blue/', 'newtab' => true, ], ] ], [ 'icon' => 'hand-o-left', 'label' => 'Three', 'url' => '/three/', 'newtab' => false, ], ]; $event->return = $children; } }); Showing/hiding menus according to user role
      You can determine which menu items can be seen by a role by checking the user's role in the hook.
      For example, if a user has or lacks a role you could include different child menu items in the hook return value. Or if you want to conditionally hide a custom menu altogether you can set the return value to false. Example:
      $wire->addHookAfter('CustomAdminMenus::getMenuChildren', function(HookEvent $event) { // The menu number is the first argument $menu_number = $event->arguments(0); $user = $event->wire()->user; // For custom menu number 1... if($menu_number === 1) { // ...if user does not have some particular role... if(!$user->hasRole('foo')) { // ...do not show the menu $event->return = false; } } });  
      https://github.com/Toutouwai/CustomAdminMenus
      https://processwire.com/modules/custom-admin-menus/
    • By tcnet
      This module for ProcessWire sends a notification email for each failed login attempt. Similar modules exists already in the module directory of ProcessWire. However, this module is designed to notify, even if specified user doesn't exist.
      Settings
      The settings for this module are located in the menu Modules=>Configure=>LoginFailNotifier.
      Notification email
      Specifies the email address to which the notification emails should be sent.
        Email subject
      Specifies the subject line for the notification email.
        Post variables
      Specifies the $_POST variables to be included in the notification email. Each variable must be separated by a comma. For example: login_name,login_pass
        Server variables
      Specifies the $_SERVER variables to be included in the notification email. Each variable must be separated by a comma. For example: REMOTE_ADDR,HTTP_USER_AGENT
      Link to ProcessWire module directory:
      https://processwire.com/modules/login-fail-notifier/
      Link to github.com:
      https://github.com/techcnet/LoginFailNotifier
    • By Fokke
      ProcessWire 3.x markup module for rendering meta tags in HTML document head section. Note that this module is not a full-blown SEO solution, but rather a simple tool for rendering meta tags based on module configuration. Adding custom meta tags is also supported.
      Built-in meta tags
      The following meta tags are supported out-of-the-box:
      Document title consisting of page title and site name Character set Canonical Viewport Description Keywords Hreflang tags Open Graph og:title og:site_name og:type og:url og:description og:image og:image:width og:image:height Twitter meta tags twitter:card twitter:site twitter:creator twitter:title twitter:description twitter:image Facebook meta tags fb:app_id The full documentation with configurable options can be found here: https://github.com/Fokke-/MarkupMetadata
       
      Requirements:
      ProcessWire>=3.0.0 PHP >=7.1 Installation using Composer
      composer require fokke/markup-metadata Manual installation
      Download latest version from https://github.com/Fokke-/MarkupMetadata/archive/master.zip Extract module files to site/modules/MarkupMetadata directory.
    • By m.sieber
      ITRK-Service for ProcessWire
      Module for the automated transfer of imprint, data protection declaration and terms and conditions from IT-Recht Kanzlei to your ProcessWire installation
      What is ITRK Service for ProcessWire?
      ITRK-Service for ProcessWire is a free module for ProcessWire CMS. It provides an interface to the update service of IT-Recht Kanzlei, via which the legal texts of your online presence are automatically updated. In this way, the texts remain legally secure and warning-proof in the long term. Imprint, data protection declaration, revocation and general terms and conditions are currently supported.
      You can find our documentation (in german language) here: https://www.pupit.de/itrk-service-for-processwire/dokumentation/

      Download: https://www.pupit.de/itrk-service-for-processwire/
      Github: https://github.com/pupit-de/pwItrkServiceConnector
    • By LuisM
      Symprowire is a PHP MVC Framework based and built on Symfony using ProcessWire 3.x as DBAL and Service-Provider
      It acts as a Drop-In Replacement Module to handle the Request/Response outside the ProcessWire Admin. Even tough Symfony or any other mature MVC Framework could be intimidating at first, Symprowire tries to abstract Configuration and Symfony Internals away as much as possible to give you a quick start and lift the heavy work for you.
      The main Goal is to give an easy path to follow an MVC Approach during development with ProcessWire and open up the available eco-system.
      You can find the GitHub Repo and more Information here: https://github.com/Luis85/symprowire
      Documentation
      The Symprowire Wiki https://github.com/Luis85/symprowire/wiki How to create a simple Blog with Symprowire https://github.com/Luis85/symprowire/wiki/Symprowire-Blog-Tutorial Last Update
      16.07.2021 // RC 1 v0.6.0 centralized ProcessWire access trough out the Application by wrapping to a Service https://github.com/Luis85/symprowire/releases/tag/v0.6.0-rc-1 Requirements
      PHP ^7.4 Fresh ProcessWire ^3.0.181 with a Blank Profile Composer 2 (v1 should work, not recommended) The usual Symfony Requirements Features
      Twig Dependency Injection Monolog for Symprowire Support for .env YAML Configuration Symfony Console and Console Commands Symfony Webprofiler Full ProcessWire access inside your Controller and Services Webpack Encore support Caveats
      Symfony is no small Framework and will come with a price in terms of Memory Usage and added Overhead. To give you a taste I installed Tracy Debugger alongside to compare ProcessWire profiling with the included Symfony Webprofiler

      So in a fresh install Symprowire would atleast add another 2MB of Memory usage and around 40ms in response time, should be less in production due to the added overhead of the Webprofiler in dev env
       
×
×
  • Create New...