Wanze

Module
Pages2Pdf

233 posts in this topic

Hi,

After reading this thread, I decided to make a module that helps generating PDF files of ProcessWire pages.

GitHub: https://github.com/wanze/Pages2Pdf

Modules Directory: http://modules.processwire.com/modules/pages2-pdf/

This module uses the mPDF library to generate the PDF files. It has fully UTF-8 and basic HTML/CSS support for rendering the PDF files.

The output is customizable with ProcessWire templates. 

Example

I've enabled generating PDF files for the skyscraper template of ryans Skyscrapers-profile with a template that outputs the data in a table along with the body text and the images:

one-atlantic-center-pdf-4177.pdf

Please take a look at the README on GitHub for instructions and further information/examples.

Cheers

Edited by Nico Knoll
Added the "module" tag and removed it from title.
32 people like this

Share this post


Link to post
Share on other sites

This will be real timesaver and super useful module, thanks Wanze!

Share this post


Link to post
Share on other sites

Cool thanks! Bisch voll im Schuss!? ;-) Why didnt this post appear in my no read list?

Share this post


Link to post
Share on other sites

Great addition Wanze! Might need this soon. Already started looking at the wat luis generated pdf's, but this seems great!

Share this post


Link to post
Share on other sites

Thanks people,

Please report feedback or ideas for further improvements.

Looks great! Would it be possible to render multiple pages in a single .pdf using this?

Yep. The pdf is generated from a ProcessWire template in /site/templates/pages2pdf/.

Think of it as a normal Pw template: You can use $page, $pages, $users and all other variables to search and output your

content. The only restriction is that you have limited html support, so make sure you use supported html

tags in TCPDF (see link in first post).

There's also another module which generates pdfs from the Admin:

http://modules.processwire.com/modules/pdfcomposer/

Share this post


Link to post
Share on other sites

Just dropping in to say that I'll definitely try this module out soon, looks very promising. Thanks for making this!

For the record, I've used mPDF in the past for creating PDF versions of pages. It's HTML and CSS support seemed superior compared to other libraries at the time, at least for my use cases. Might have to switch one site to use this module instead of mPDF just to see how they really compare.

Taking a look at your code I'm seeing quite a bit similarities with these libraries; for an example seem to have identical method names. Interesting. :)

1 person likes this

Share this post


Link to post
Share on other sites

Hi teppo,

Thanks.

This is very interesting, I didn't know mPDF before. Looks very promising if the html support is better plus the whole

library is very lightweight compared toTCPDF.

TCPDF and also mPDF are both extensions of FPDF. Some years ago, I used FPDF but the problem there

is the missung UTF-8 and HTML support.

As most of the libraries seem to depend on FPDF, this now has the advantage that one could easily exchange the library behind the module without changing much code.

Looking forward to your feedback! :)

1 person likes this

Share this post


Link to post
Share on other sites

Great module, I'll give it a good use.

Thank you for sharing Wanze.

1 person likes this

Share this post


Link to post
Share on other sites

Thanks manviny!

If you have ideas for improvements, please share.

Haven't used the module myself actually :)

Cheers

Share this post


Link to post
Share on other sites

Hi Wanze, great work and very useful! :)

I have one question: do you plan to embedd the FPDI-Extension into it?

I know it from working with FPDF. In the past FPDI-Extension couldn't bundled with TCPDF a long time because of the licence, but this has changed a few year ago.
 

Would be useful to upload or otherwise specify a PDF as background on a 'per-Template-Basis'. (<= uh, ugly Denglish)

Share this post


Link to post
Share on other sites

Horst - thanks! :)

Never heard of the FPDI extension. I checked your link and read some docs on the website, but I'm not sure

how this should be integrated here.

Can you give me more details? :)

What do you mean with "background"? As far as I understand, FPDI is used to import pages from other

pdf's into your pdf.

Grüsse nach Deutschland!

Share this post


Link to post
Share on other sites

Horst - thanks! :)

Never heard of the FPDI extension. I checked your link and read some docs on the website, but I'm not sure

how this should be integrated here.

Can you give me more details? :)

What do you mean with "background"? As far as I understand, FPDI is used to import pages from other

pdf's into your pdf.

Grüsse nach Deutschland!

Hi Wanze, yes with the FPDI extension you can import other PDFs, but in different ways. What I mean with 'as Background' and what I'm find very useful is, for example:

You can take a PDF with your e.g. Briefkopf , assuming you have a first page with all your stuff (Logo, adress, bankinfo, tax number, etc.) and a second page with less data (maybe a smaller version of your Logo and only name and phone number), and use it as background-layers:

// example use of FPDI-Extension with FPDF.

	$tplfile = 'C:/fpdf/templates/mein_briefkopf.pdf';
	$pagecount = $this->pdf->setSourceFile($tplfile);              // defining the sourcefile returns Pagenumber of it
	for($i=1;$i<=$pagecount;$i++)
	{
		$this->fpdiTemplates['tpl_s'.$i] = $this->pdf->ImportPage($i);     // import each page and assign it to a variable
	}
	$this->Parser_Opened = TRUE;
	$this->Use_Template = $this->fpdiTemplates['tpl_s1'];
	...                                                            // write own stuff to the first page
                                                                       // switch the Template to the second page for all
                                                                       // following pages (optional)
        $this->Use_Template = $this->fpdiTemplates['tpl_s2'];
        ... // create more pages

With the use of FPDI-Templates you can add graphical stuff to your pdf-pages with less effort.

On my website one can add images to a virtual lightbox and can download them as a PDF.

There are three template pages: one for the image pages, one with my contact-data and copyright notice! and optional one for user notes.

..., und viele Grüße in die Schweiz :)

3 people like this

Share this post


Link to post
Share on other sites

Wow horst - that is super awesome; I'll have to revisit this, because i need something like this for a site i'm currently working on, where there is a product and we want to generate a slick product sheet, downloadable pdf using the existing PW content and images...

Share this post


Link to post
Share on other sites

Hi Wanze, thanks for all your work on this, I'm excited to try it out.

Problem I have at the moment is the render call isn't outputting anything. I've definitely added my template in the module settings but there's no link or anything getting outputted.

Have you seen this issue before?

Thanks.

Share this post


Link to post
Share on other sites

Hi onjegolders,

I just noticed that I forgot to add an 'echo' in front of the render call in my first post - updated.

It should be:

echo $modules->get('Pages2Pdf')->render();

Was this the issue? (hope so... :))

Share this post


Link to post
Share on other sites

Yeah that sorted it thanks Wanze, I should have seen that!

Do you know what differences there are between this implementation and using HTML2PDF?

I have a very basic usage of that library and it seems to work "OK" but I'm guessing a PW way is better. Are there any other differences that you know of?

Thanks again for all your hard work Wanze.

Share this post


Link to post
Share on other sites

No problem - thanks you for finding this one! ^-^

I've never used html2pdf before, but it looks like it's an addition on top of tcpdf - the framework that this module is using.

Don't know the differences exactly. This module just integrates TCPDF into ProcessWire, making it easier for you to setup

everything. However, it could be the case that html2pdf has better html support built in for rendering the pdfs than tcpdf.

The module handles creating / caching pdf's for you and you can output the content with Pw templates. This means,

you have the full Pw API available if needed.

If anyone can confirm that other libraries, e.g. html2pdf or mPDF have actually better html support than TCPDF, I'll switch the

framework behind the module.

1 person likes this

Share this post


Link to post
Share on other sites
hi wanze, i have a problem with this module.

When i use english characters is working fine and it exports successfully the pdfs.

But with the greek chars i get question marks.

is there any solution for this; i have search the tcpdf examples but i can't make it  to work

thanks.

Share this post


Link to post
Share on other sites

Hi sakkoulas,

That's because the standard font set by the module does not have those characters included. I removed all fonts but the standard ones from the tcpdf folder so the module doesn't get too heavy.

You can do those steps:

  1. Download TCPDF
  2. Copy all files starting with freesans* or freeserif* to /site/modules/Pages2Pdf/tcpdf/fonts
  3. In your module configs, set freesans/freeserif under Default font

Then the greek characters work (make sure you enable debug mode or save the page to delete any cached pdfs).

I think this is the easiest solution. Disadvantage is that the pdf is a bit heavier due to this font which includes all unicode characters.

Another possibility is to convert a greek font to TCPDF according to the docs: http://www.tcpdf.org/fonts.php

Does this work for you?

2 people like this

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.

  • Similar Content

    • By Robin S
      First a note about my other modules...
      I have three existing modules that are similar in that they allow restrictions to be placed on repeating inputfields: Limit Repeater, Limit PageTable, Limit Table
      Restrict Repeater Matrix takes a different approach to the module configuration from those other modules. The module settings for Restrict Repeater Matrix are applied in the field settings rather in a module config screen. I think this new approach is better, but it means that it isn't practical to create different settings for different roles via the admin interface. Instead the module has a hookable method, allowing roles to be targeted and other advanced usages to be achieved via a hook. The result is that the module is more flexible.
      I intend to transition my other modules to the same approach over the coming weeks, but because this will result in breaking changes I will be releasing the updated modules under new names ("Restrict Repeater", etc) to avoid users upgrading via the Upgrades module without full awareness of the changes. The old modules will be marked as deprecated.
       
      Restrict Repeater Matrix
      A module for ProcessWire CMS/CMF. Allows restrictions and limits to be placed on Repeater Matrix fields.
      For any matrix type in a Repeater Matrix field you have the option to:
      Prevent drag-sorting of items Prevent cloning of items Prevent toggling of the published state of items Prevent trashing of items Limit the number of items that may be added to the inputfield Please note that restrictions and limits are applied with CSS/JS so should not be considered tamper-proof.
      Usage
      Install the Restrict Repeater Matrix module.
      For each matrix type created in the Repeater Matrix field settings, a "Restrictions" fieldset is added at the bottom of the matrix type settings:

      For newly added matrix types, the settings must be saved first in order for the Restrictions fieldset to appear. Set restrictions for each matrix type as needed. A limit of zero means that no items of that matrix type may be added to the inputfield.
      Setting restrictions via a hook
      Besides setting restrictions in the field settings, you can also apply or modify restrictions by hooking RestrictRepeaterMatrix::checkRestrictions. This allows for more focused restrictions, for example, applying restrictions depending on the template of the page being edited or depending on the role of the user.
      The checkRestrictions() method receives the following arguments:
      $field This Repeater Matrix field $inputfield This Repeater Matrix inputfield $matrix_types An array of matrix types for this field. Each key is the matrix type name and the value is the matrix type integer. $page The page that is open in ProcessPageEdit The method returns a multi-dimensional array of matrix types and restrictions for each of those types. An example of a returned array:

      Example hooks
      Prevent the matrix type "images_block" from being added to "my_matrix_field" in a page with the "basic-page" template:
      $wire->addHookAfter('RestrictRepeaterMatrix::checkRestrictions', function(HookEvent $event) { $field = $event->arguments('field'); $page = $event->arguments('page'); $type_restrictions = $event->return; if($field->name === 'my_matrix_field' && $page->template->name === 'basic-page') { $type_restrictions['images_block']['limit'] = 0; } $event->return = $type_restrictions; });  
      Prevent non-superusers from trashing any Repeater Matrix items in "my_matrix_field":
      $wire->addHookAfter('RestrictRepeaterMatrix::checkRestrictions', function(HookEvent $event) { $field = $event->arguments('field'); $type_restrictions = $event->return; if($field->name === 'my_matrix_field' && !$this->user->isSuperuser()) { foreach($type_restrictions as $key => $value) { $type_restrictions[$key]['notrash'] = true; } } $event->return = $type_restrictions; });  
      https://github.com/Toutouwai/RestrictRepeaterMatrix
    • By bernhard
      Hi,
      just stumbled over a little module that i built for my last project. it helped me to test performance of my rockdatatables module to generate 3000 random json datasets and i want to share it with you. maybe it saves some time for someone.
      https://gitlab.com/baumrock/RockDummyData/
      easy example:
      $rdd = $modules->get('RockDummyData'); for($i=0; $i<15; $i++) { // this has to be inside the for-loop to always get a new dummy $dummy = $rdd->getDummy(); echo date("d.m.Y H:i:s", $dummy->timestamp) . "<br>"; } more advanced:
      $json = new stdClass(); $json->data = array(); $rdd = $modules->get('RockDummyData'); for($i=0; $i<3000; $i++) { // this has to be inside the for-loop to always get a new dummy $dummy = $rdd->getDummy(); $obj = new stdClass(); $obj->name = $dummy->forename . ' ' . $dummy->surname; $obj->position = $dummy->job; $obj->office = $dummy->city; $obj->color = $dummy->color; $obj->start_date = new stdClass(); $obj->start_date->display = date('d.m.Y',$dummy->timestamp); $obj->start_date->sort = $dummy->timestamp; $obj->salary = rand(0,10000); $json->data[] = $obj; } echo json_encode($json); you have to store your random datasets on your own into the /data folder. there are several services for creating all kinds of random data on the web - if you know one service that allows sharing those datasets let me know and i can include common needed data into the module
    • By AndySh
      Hello!
      I need your assistance please. I purchased the module FormBuilder. Unfortunately, the module discontinued delivering customer submissions to e-mail box specified in the module settings. Direct mailing to the e-mail box works OK. The module settings stays the same and are correct, like "Send e-mail to administrator(s) is checked. The last version of FormBuilder 3.0 has been installed. Please advise how to resolve the issue becase I cannot get orders from customers anymore (((
    • By kixe
      As described in this post (https://processwire.com/talk/topic/8551-custom-urls-for-pages/?p=82742) the option 'Name Format Children' under the tab 'Family' in template settings doesn't work properly and also not as expected. I had a look inside the code and made some changes which are working properly, which offers much more options, more consistency and less code too.

      The result is the following. You have 3 Options for generating name and title, which could be combined in endless variations.
      Name is always derived from title, same like creating pages manually.
      type date: if function detects # character anywhere in the string, conversion will be: deletion of # and string will be used as format parameter for PHP date() function type field: if string is a fieldname of the parent page the value of this field will be used type string: if string doesn't fit to the 2 preceeding it will be taken as it is All parts (separated by comma) will be composed in the order of setting. You can use unlimited numbers of parts

      I made a pull request on github: https://github.com/ryancramerdesign/ProcessWire/pull/831

      Example screenshots

      Setting ...


      will result in


       
    • By kongondo
      FieldtypeRuntimeMarkup and InputfieldRuntimeMarkup
       
      Modules Directory: http://modules.processwire.com/modules/fieldtype-runtime-markup/
      GitHub: https://github.com/kongondo/FieldtypeRuntimeMarkup
       
      This module allows for custom markup to be dynamically (PHP) generated and output within a page's edit screen (in Admin).
       
      The value for the fieldtype is generated at runtime. No data is saved in the database. The accompanying InputfieldRuntimeMarkup is only used to render/display the markup in the page edit screen.
       
      The field's value is accessible from the ProcessWire API in the frontend like any other field, i.e. it has access to $page and $pages.
       
      The module was commissioned/sponsored by @Valan. Although there's certainly other ways to achieve what this module does, it offers a dynamic and flexible alternative to generating your own markup in a page's edit screen whilst also allowing access to that markup in the frontend. Thanks Valan!
       
      Warning/Consideration
      Although access to ProcessWire's Fields' admin pages is only available to Superusers, this Fieldtype will evaluate and run the custom PHP Code entered and saved in the field's settings (Details tab). Utmost care should therefore be taken in making sure your code does not perform any CRUD operations!! (unless of course that's intentional) The value for this fieldtype is generated at runtime and thus no data is stored in the database. This means that you cannot directly query a RuntimeMarkup field from $pages->find(). Usage and API
       
      Backend
      Enter your custom PHP snippet in the Details tab of your field (it is RECOMMENDED though that you use wireRenderFile() instead. See example below). Your code can be as simple or as complicated as you want as long as in the end you return a value that is not an array or an object or anything other than a string/integer.
       
      FieldtypeRuntimeMarkup has access to $page (the current page being edited/viewed) and $pages. 
       
      A very simple example.
      return 'Hello'; Simple example.
      return $page->title; Simple example with markup.
      return '<h2>' . $page->title . '</h2>'; Another simple example with markup.
      $out = '<h1>hello '; $out .= $page->title; $out .= '</h1>'; return $out; A more advanced example.
      $p = $pages->get('/about-us/')->child('sort=random'); return '<p>' . $p->title . '</p>'; An even more complex example.
      $str =''; if($page->name == 'about-us') { $p = $page->children->last(); $str = "<h2><a href='{$p->url}'>{$p->title}</a></h2>"; } else { $str = "<h2><a href='{$page->url}'>{$page->title}</a></h2>"; } return $str; Rather than type your code directly in the Details tab of the field, it is highly recommended that you placed all your code in an external file and call that file using the core wireRenderFile() method. Taking this approach means you will be able to edit your code in your favourite text editor. It also means you will be able to type more text without having to scroll. Editing the file is also easier than editing the field. To use this approach, simply do:
      return wireRenderFile('name-of-file');// file will be in /site/templates/ If using ProcessWire 3.x, you will need to use namespace as follows:
      return ProcessWire\wireRenderFile('name-of-file'); How to access the value of RuntimeMarkup in the frontend (our field is called 'runtime_markup')
       
      Access the field on the current page (just like any other field)
      echo $page->runtime_markup; Access the field on another page
      echo $pages->get('/about-us/')->runtime_markup; Screenshots
       
      Backend
       

       

       
      Frontend