ElkArte Community

Extending Elk => Addons => Topic started by: Spuds on August 14, 2014, 03:28:53 am

Title: [ADDON] PDF Print
Post by: Spuds on August 14, 2014, 03:28:53 am
This addon replaces the print button functionality (in a topic view) such that it creates a PDF suitable for saving or printing.  There are no options, just install the addon and then when your select print it will generate and display a PDF instead of a print page.

I really did this as an experiment to play with FPDF so it only supports html basic tags and also includes image attachments in the PDF.  So nothing fancy but seems to work in some basic testing, YMMV ;)

Print to PDF


License
This Elkarte addon is released under a BSD-3-Clause license.

Introduction
This addon replaces the print button action to instead create a PDF of the topic for viewing / saving.

Dependencies
Utilizes tFPDF (based on FPDF 1.7) by Ian Back, License:  LGPL                      

Features
 o One-click creation of topic PDF's
 o Includes topic image attachments in the PDF
 o Maintains basic text formatting
 o No source edits, all done with hooks.

Repo
https://bitbucket.org/spuds_/elk-pdf
Source
https://bitbucket.org/spuds_/elk-pdf/src
Download
http://addons.elkarte.net/enhancement/Print-PDF.html
Title: Re: [ADDON] PDF Print
Post by: emanuele on August 14, 2014, 09:48:05 am
Coolnessssss! :D

/me likes when Spuds does experiments! ^-^
Title: Re: [ADDON] PDF Print
Post by: overscan on October 02, 2014, 11:37:26 am
Supercool idea ideal for my forum. However, when installed, it doesn't work.

On Chrome I get Cannot be displayed:

Error code: ERR_CONTENT_DECODING_FAILED
Title: Re: [ADDON] PDF Print
Post by: emanuele on October 02, 2014, 11:54:57 am
Searching the net it looks like a compression issue.
I think Spuds should "copy" the attachments download code and add something like that:
Code: [Select]
		if (!empty($modSettings['enableCompressedOutput']) && strlen($out) <= 4194304)
ob_start('ob_gzhandler');
else
{
ob_start();
header('Content-Encoding: none');
}
after the ob_end_clean loop the file creation at:
https://bitbucket.org/spuds_/elk-pdf/src/327b68d7031f11dd8629fa3c17b6f5055474084d/sources/PDF.controller.php?at=master#cl-173 (line 173).
Title: Re: [ADDON] PDF Print
Post by: overscan on October 02, 2014, 12:47:23 pm
Yep - disabling compression fixes the error. Nice.
Title: Re: [ADDON] PDF Print
Post by: Spuds on October 02, 2014, 03:08:04 pm
Cool, thanks for the debug, will make the update  :D
Title: Re: [ADDON] PDF Print
Post by: meetdilip on October 02, 2014, 11:38:58 pm
/me thinks it is a helpful add on
Title: Re: [ADDON] PDF Print
Post by: overscan on February 06, 2015, 02:46:24 am
So, I'm back on forum conversion from SMF after finishing my book.

This plugin is almost great but breaks if any image in the topic is missing or has any kind of error. Instead of a pdf you get lot of code and at the bottom something like:

FPDF error: Unsupported image type
FPDF error: can't find image xxxxx

This includes links to external images outside of the forum's control.

Can't do that to my users, pity as the PDF output is nice when it works.

At least trapping the error and saying "couldn't generate PDF due to missing images" would be essential.
Title: Re: [ADDON] PDF Print
Post by: emanuele on February 07, 2015, 02:20:45 pm
Removed... wrong tab when posting about ILA... :-[
Title: Re: [ADDON] PDF Print
Post by: Spuds on February 07, 2015, 10:54:41 pm
So, I'm back on forum conversion from SMF after finishing my book.

This plugin is almost great but breaks if any image in the topic is missing or has any kind of error. Instead of a pdf you get lot of code and at the bottom something like:

FPDF error: Unsupported image type
FPDF error: can't find image xxxxx

This includes links to external images outside of the forum's control.

Can't do that to my users, pity as the PDF output is nice when it works.

At least trapping the error and saying "couldn't generate PDF due to missing images" would be essential.
I'll take a look to see what I can do ... If I can trap the error I may just be able to replace it with the link (even if its bad) ... All depends on what FPDF is doing.
Title: Re: [ADDON] PDF Print
Post by: Spuds on February 08, 2015, 11:40:00 pm
Did some updates so it will better handle external images, give it a whirl (download at the OP link)
Title: Re: [ADDON] PDF Print
Post by: Jorin on March 10, 2017, 06:42:07 am
I installed this addon today. Thanks for it!  :)

Is it possible to change the addon so the PDF is opened in a new tab/window? And maybe it's better if the button says "PDF" instead of "Print"?

Edit: I get an error:

Quote
FPDF error: Interlacing not supported: attachments/11377_087368c3070226df31fd8fbcd4a6c00814707a5c.elk

There are some PNG files in the thread.
Title: Re: [ADDON] PDF Print
Post by: Spuds on March 12, 2017, 04:07:43 am
Could you point me to the image that is causing the problem?
Title: Re: [ADDON] PDF Print
Post by: Jorin on March 13, 2017, 07:00:39 am
Must be in this thread:

http://forum-alternative-antriebe.de/index.php/topic,6580.0.html

If you try to make a PDF out of it (button is called "PDF"), the error appears.
Title: Re: [ADDON] PDF Print
Post by: Spuds on March 13, 2017, 02:23:23 pm
Strange ... I've copied all the images to my local and I can't duplicate the error.  I know where in the code the error would occur, I just can't duplicate it :(  What version of PHP and are you using GD or Imagick on your system?
Title: Re: [ADDON] PDF Print
Post by: Jorin on March 13, 2017, 02:36:41 pm
Okay, it's not a browser problem, I have the error with both IE and Chrome. Phew! But maybe it's a problem with the image resize addon, which I installed at the same time? Sadly I don't know if the problem appeared before or after the image resize addon installation (http://www.elkarte.net/community/index.php?topic=1372.msg31176#msg31176).

It is PHP 5.6.29-nmm1 and Imagick 3.4.1. And GD seems to be there too, bundled (2.1.0 compatible).
Title: Re: [ADDON] PDF Print
Post by: Spuds on March 13, 2017, 03:06:34 pm
Was finally able to duplicate the issue ... now on to fix it !
Title: Re: [ADDON] PDF Print
Post by: Spuds on March 13, 2017, 08:56:03 pm
New version to test .. uninstall / delete the old and upload / install this one.

This also changes the button to say PDF, so whatever edit you made to do that should no longer be needed.
Title: Re: [ADDON] PDF Print
Post by: Jorin on March 13, 2017, 09:34:13 pm
Thank you, I will try it tomorrow.
Title: Re: [ADDON] PDF Print
Post by: Jorin on March 14, 2017, 06:57:23 am
Thank you, it works with the new version!  :)
Title: Re: [ADDON] PDF Print
Post by: Spuds on November 26, 2017, 11:47:39 pm
Updated package to support ElkArte 1.1
Title: Re: [ADDON] PDF Print
Post by: Jorin on June 07, 2018, 01:44:11 pm
When clicking as a guest:

Code: [Select]
Fatal error: Call to undefined function fatal_lang_error() in /www/htdocs/xyz/hp_elkarte/sources/controllers/PDF.controller.php on line 55
Title: Re: [ADDON] PDF Print
Post by: Spuds on June 07, 2018, 10:51:52 pm
Yup ... thats what we call a bug :D

I'll fix that shortly, but I'm also going to "hide" the PDF button for guests since its not supposed to work for them anyway.
Title: Re: [ADDON] PDF Print
Post by: Spuds on June 08, 2018, 08:05:22 pm
I updated the package, it should now be available on the addon site.
Title: Re: [ADDON] PDF Print
Post by: Jorin on June 10, 2018, 07:28:13 am
Okay, but the button "print" (not PDF!) as a guest is still there. It seems this is the old function? I can't disable "print for guests" because there is no such setting in the groups permissions for guests.
Title: Re: [ADDON] PDF Print
Post by: Spuds on June 11, 2018, 05:54:40 am
Yeah I noted that the print button was still there (that has nothing to do with the addon).   I think to remove that you have to disable the print topic feature which I think is under the topic settings in the ACP.
Title: Re: [ADDON] PDF Print
Post by: Jorin on June 11, 2018, 07:14:39 am
If I do that, your PDF button is gone too.  :D
Title: Re: [ADDON] PDF Print
Post by: Spuds on June 12, 2018, 04:14:15 am
Yes ... I think the option is simply on or off and its not a permission based feature, kind of strange really.
Title: Re: [ADDON] PDF Print
Post by: Spuds on May 04, 2021, 07:13:22 pm
Update to version 1.0.8

 - Updates to latest tfpdf library with better php 7.4 support.
 - Adds basic limited support for inline images/attachments.
 - Adds basic support for levertine gallery images.
 - Fixes several layout issues with below post attachments.

Note, gallery and inline images are only rendered as left aligned images, so what you see and what you get will be  different.  There are many cases/variations and I did not want deal with them all.  PR's are welcome if you want to improve on how images are handled.

See the first post for the link to the addon site
Title: Re: [ADDON] PDF Print
Post by: optimizerdude on July 14, 2021, 05:02:54 am
Can you add the ability to disable images all together for PDF Print?
Can you add the ability to see and use this addon based on permissions?
Thank you!
Title: Re: [ADDON] PDF Print
Post by: Spuds on July 14, 2021, 03:49:37 pm
The permission to use the PDF function is, critically, tied to the "send topics to friends".  Remove that permission and the PDF function should be disabled for those users.

I could easily add a feature to disable the images in the output, I'll see if I have time this weekend.  If you are in a rush you can edit the elk-pdf class and set the validImageTypes variable to an empty array.