Wanze

Pages2Pdf

Recommended Posts

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

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

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

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. :)

  • Like 1

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! :)

  • Like 1

Share this post


Link to post
Share on other sites

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

Thank you for sharing Wanze.

  • Like 1

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 :)

  • Like 3

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.

  • Like 1

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?

  • Like 2

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 BitPoet
      As threatened in the Pub sub forum in the "What are you currently building?" thread, I've toyed around with Collabora CODE and built file editing capabilities for office documents (Libre-/OpenOffice formats and MS Office as well as a few really old file types) into a PW module.
      If you are running OwnCloud or NextCloud, you'll perhaps be familiar with the Collabora app for this purpose.
      LoolEditor
      Edit office files directly in ProcessWire
      Edit your docx, odt, pptx, xlsx or whatever office files you have stored in your file fields directly from ProcessWire's page editor. Upload, click the edit icon, make your changes and save. Can be enabled per field, even in template context.
      Currently supports opening and saving of office documents. Locking functionality is in development.
      See the README on GitHub for installation instructions. You should be reasonably experienced with configuring HTTPS and running docker images to get things set up quickly.
      Pull requests are welcome!
      Here is a short demonstration:

    • By Robin S
      An Images field allows you to:
      Rename images by clicking the filename in the edit panel or in list view. Replace images, keeping metadata and filename (when possible) by dropping a new image on the thumbnail in the edit panel. Introduced here. But neither of these things is possible in File fields, which prompted this module. The way that files are renamed or replaced in this module is not as slick as in the Images field but it gets the job done. The most time-consuming part was dealing with the UI differences of the core admin themes. @tpr, gives me even more respect for the work that must go into AdminOnSteroids.
      Most of the code to support the rename/replace features is already present in InputfieldFile - there is just no UI for it currently. So hopefully that means these features will be offered in the core soon and this module can become obsolete.
       
      Files Rename Replace
      Allows files to be renamed or replaced in Page Edit.

      Usage
      Install the Files Rename Replace module.
      If you want to limit the module to certain roles only, select the roles in the module config. If no roles are selected then any role may rename/replace files.
      In Page Edit, click "Rename/Replace" for a file...
      Rename
      Use the text input to edit the existing name (excluding file extension).
      Replace
      Use the "Replace with" select to choose a replacement file from the same field. On page save the file will be replaced with the file you selected. Metadata (description, tags) will be retained, and the filename also if the file extensions are the same.
      Tip: newly uploaded files will appear in the "Replace with" select after the page has been saved.
       
      https://github.com/Toutouwai/FilesRenameReplace
      http://modules.processwire.com/modules/files-rename-replace/
    • By d'Hinnisdaël
      So I decided to wade into module development and created a wrapper module around ImageOptim, a service that compresses and optimizes images in the cloud. ImageOptim currently handles JPG, PNG and GIF files and, depending on the settings you use, shaves off between 15% and 60% in filesize. Great for bandwidth and great for users, especially on mobile.
      This module handles the part of uploading images to ImageOptim via their official API, downloading the optimized version and storing it alongside the original image.
       

       
      Download & Info
      GitHub / Module directory / Readme / Usage
       
      Why ImageOptim?
      There are other image optimization services out there, some of them free, that have outstanding ProcessWire modules. A few things make ImageOptim the best tool for most of my customers: It's not free, i.e. it will probably be around for a while and offers support. However, it's cheaper than some of the bigger competitors like Cloudinary. And it does PNG compression better than any of the free services out there, especially those with alpha channels.
       
      Installation
      Install the module like any other ProcessWire module, by either copying the folder into your modules folder or installing it via the admin. See above for downloads links on GitHub and in the module directory.
       
      Requirements
      To be able to upload images to the service, allow_url_fopen must be set on the server. The module will abort installation if that's not the case.
      I have only tested the module on ProcessWire 3.x installations. I don't see why it shouldn't work in 2.x, if anyone wants to try it out and report back.
       
      ImageOptim account
      To compress images, you first need to sign up for an ImageOptim account. They offer free trials to try the service.
       
      Usage (manual optimization)
      Images can be optimized by calling the optimize() method on any image. You can pass an options array to set ImageOptim API parameters.
      $image->size(800,600)->optimize()->url $image->optimize(['quality' => 'low', 'dpr' => 2]) // Set quality to low and enable hi-dpi mode
      Automatic optimization
      The module also has an automatic mode that optimizes all image variations after resizing. This is the recommended way to use this module since it leaves the original image uncompressed, but optimizes all derivative images.
      $image->size(800,600)->url // nothing to do here; image is optimized automatically
      To change compression setting for single images, you can pass an options array along with the standard ImageResizer options. Passing false disables optimization.
      $image->size(800, 600, ['optimize' => 'medium']) $image->size(800, 600, ['optimize' => ['quality' => 'low', 'dpr' => 2]]) $image->size(800, 600, ['optimize' => false])
      For detailed usage instructions and all API parameters, see the usage instructions on GitHub.
       
      Filenames
      Optimized images will be suffixed, e.g. image.jpg becomes image.optim.jpg. You can configure the suffix in the module settings.
       
      Roadmap
      Asynchronous processing. Not really high on the list. Image variations need to be created anyway, so waiting a few seconds longer on first load is preferable to adding complexity to achieve async optimization. Optimize image variations created by other modules. CroppableImage comes to mind. I don't use any of these, so if somebody wants to help out and submit a pull request — all for it! Add a dedicated page in the setup menu with a dashboard and detailed statistics. ImageOptim's API is very barebones for now, so not sure if that's feasible or even necessary.
      Stability
      I've been using this module on production sites for some time now, without hiccups. If you do notice oddities, feel free to comment here or investigate and submit PRs.
    • By daniels
      General
      This is a lightweight alternative to other newsletter & newsletter-subscription modules.
      It can update / create a subscriber in a list in Mailchimp with MailChimp API 3.0. It does not provide any forms or validation, so you can feel free to use your own. To protect your users, it does not save any user data in logs or sends them to an admin.
      This module fits your needs if you...
      ...use Mailchimp as your newsletter / email-automation tool ...want to let users subscribe to your newsletter on your website ...want to use your own form, validation and messages (with or without the wire forms) ...don't want any personal user data saved in any way in your ProcessWire environment (cf. EU data regulation terms) ...like to subscribe users to different lists ...like the Mailchimp UI for creating / sending / reviewing email campaigns You can find it here: https://github.com/danielstieber/SubscribeToMailchimp
      Let me know what you think and if I should add it to the Modules Directory.
      Setup
      Log into your Mailchimp account and go to  Profile > Extras > API Keys. If you don't have an API Key, create a new one. Copy your API Key and paste it in the module settings (Processwire > Modules > Site > SubscribeToMailchimp). Back in Mailchimp, go to the list, where you want your new subscribers. Go to Settings > List name and defaults. Copy the List ID an paste it in to the module settings.
      Usage
      To use the module, you need to load it into your template:
      $mc = $modules->get("SubscribeToMailchimp"); Now you can pass an email address to the module and it will try to edit (if the user exists) or create a new subscriber in your list.
      $mc->subscribe('john.doe@example.com'); You can also pass a data array, to add additional info.
      $mc->subscribe('john.doe@example.com', ['FNAME' => 'John', 'LNAME' => 'Doe']); You can even choose an alternative list, if you don't want this subscriber in your default list.
      $mc->subscribe('john.doe@example.com', ['FNAME' => 'John', 'LNAME' => 'Doe'], 'abcdef1356'); // Subscribe to List ID abcdef1356  
      Important Notes
      This module does not do any data validation. Use a sever-sided validation like Valitron Make sure that you have set up your fields in your Mailchimp list. You can do it at Settings > List fields and *|MERGE|* tags Example
      Example usage after a form is submitted on your page:
      // ... validation of form data $mc = $modules->get("SubscribeToMailchimp"); $email = $input->post->email; $subscriber = [ 'FNAME' => $input->post->firstname, 'LNAME' => $input->post->lastname, ]; $mc->subscribe($email, $subscriber);  
      Troubleshooting
      In case of trouble check your ProcessWire warning logs.
      I can't see the subscriber in the list
      If you have enabled double opt-in (it is enabled by default) you will not see the subscriber, until he confirmed the subscription in the email sent by Mailchimp
      I get an error in my ProccessWire warning logs
      Check if you have the right List ID and API Key. Check if you pass fields, that exist in your list. Check if you pass a valid email address. Go to Mailchimps Error Glossary for more Information
      How To Install
      Download the zip file at Github or clone directly the repo into your site/modules If you downloaded the zip file, extract it in your sites/modules directory. You might have to change the folders name to 'SubscribeToMailchimp'. Goto the modules admin page, click on refresh and install it  
    • By PWaddict
      Module for hiding pages for non-superusers.
      Download
      HidePages
      Requirements
      ProcessWire 3.x Changelog
      1.0.1 (31 March 2018)
      Module renamed to prevent confusion 1.0.0 (30 March 2018)
      Initial release