Jump to content
August

generating PDF-Documents with images

Recommended Posts

Hi again, after trying pages2PDF, RockPDF and mpdf I'm not sure which of them I should use. Of course, there is also wirePDF.

I merged mpdf with rockPDF, add a footer and a page in Landscape and that works, like in the code-snippet below but be am I on the right way?

After that I tried a couple of times rendering an image. Nothing happend, the same with Pages2PDF too. I also copied an Image-URL within processwire and tried further getting an Image within the PDF-Document, but it still display only the red marked placeholder.

I simply would like create an PDF on click from some Pages including the Images.

Are there special requirements on Debian 10 with Apache and php 7.3 using Processwire 3.0.123.?
Is there a guide that I didn't found?
Is somebody out there who has a similar issue solved and would you be so kind to explain?

 

        $pdf = $modules->get('RockPdf');
        $mpdf = $pdf->mpdf;
#        $mpdf->showImageErrors = true;
        $pdf->set('SetHeader', 'header text');
        $mpdf->AddPage('L');
#        $mpdf->Image(urlencode('site/assets/files/1845/image-2.275x200.jpg'), 0, 0, 210, 297, 'jpg', '', true, false);
        $pdf->set('SetFooter', '
<table width="100%">
    <tr>
        <td width="33%">{DATE j.m.Y}</td>
        <td width="33%" align="center">{PAGENO}/{nbpg}</td>
        <td width="33%" style="text-align: right;">whatever</td>
    </tr>
</table>');
        $pdf->show(); // generate pdf

 

Share this post


Link to post
Share on other sites
$img = $pages->get(1)->images->first(); // adjust to your setup
$pdf = $modules->get('RockPdf');
$mpdf = $pdf->mpdf;
$mpdf->WriteHTML('Hello World ' . date('H:i:s'));
$mpdf->WriteHTML('<div>Showing image ' . $img->url . '</div>');
$mpdf->WriteHTML('<img src="' . $img->url . '">');
$pdf->save();

Does this work?

  • Like 1

Share this post


Link to post
Share on other sites

image.png.bbf7541df3ab82cfd0efb4d1414639e1.png

Hi bernhard,

unfortunately, no. Thank you.
The reason for this could be the URL path, the image is only accessible via the complete path (localhost).
This adress in the browser does not work:
"file:///www/home/in..."

 

 

Share this post


Link to post
Share on other sites
13 hours ago, August said:

After that I tried a couple of times rendering an image. Nothing happend, the same with Pages2PDF too. I also copied an Image-URL within processwire and tried further getting an Image within the PDF-Document, but it still display only the red marked placeholder.

I think you need to pass the full (absolute) filename to the mpdf->image() method, not a relative one and not(!) a URL.

$filename = "/absolute/filepath/to/the/image.jpg";

// or when using PWs pageimage
$filename = $image->filename;

// do a test in your script for debugging:
if(!is_readable($filename)) die('Missing file');

$mpdf->AddPage('L');
$mpdf->Image($filename, 0, 0, 210, 297, 'jpg', ...

 

  • Like 2

Share this post


Link to post
Share on other sites

You need to use the disk path instead of the URL

foreach ($this->page->getUnformatted($fieldName) as $img) {
	echo "<img src='{$img->filename}'>";
}

 

  • Like 2

Share this post


Link to post
Share on other sites
4 hours ago, Jens Martsch - dotnetic said:

You need to use the disk path instead of the URL

I'm using ->url in several spots in my projects and it works. Maybe he messed up something else or maybe our setups are just different. The screenshot looks like he is already using a full disc path /var/www/... and not an url site/assets/...

@August Make sure the file exists on your server and then just try different paths/urls and see which one works for you. Start doing it manually without using pw variables and once you have one working example expand on that one.

  • Like 1

Share this post


Link to post
Share on other sites

Well, yeah, I am using path because of different reasons. Because my images are not available via URL (or only if logged in) as I use pagefileSecure in my config. So the process mPDF has no access to the images, and therefore I use the path.

  • Like 1

Share this post


Link to post
Share on other sites

Hey, lets pull it down to this:

serving the full filesystempath should work everytime, if the file is_readable(),

URL „may“ work most of the time, as the pdf-lib will expand it to the full filesystempath. 

In both scenarios finally the full filesystempath is needed to read, parse, import the imagefile into the pdf object. 

😉

  • Like 1

Share this post


Link to post
Share on other sites
2 hours ago, Jens Martsch - dotnetic said:

Well, yeah, I am using path because of different reasons. Because my images are not available via URL (or only if logged in) as I use pagefileSecure in my config. So the process mPDF has no access to the images, and therefore I use the path.

Nice to know, thx. I haven't been working with pdf for some time but I remember I've always had issues with the correct file paths... I think paths did somehow not work, but not sure. I think they didn't work in older versions of mpdf. There you had to define relative paths (not public urls) to a root path that was set in the module's config. Maybe things have changed to the good 🙂 

  • Like 1

Share this post


Link to post
Share on other sites

Hey Guys, thanks a lot for your replies. Back after work, I have just checked your answers and I'm closely suprised. It is the indication of the absolute path that the image outputs immediately. Bernhards snippet - an excerpt, I took again. Then I only changed one line as in the code-snippet below, that solved the problem. .

$mpdf->WriteHTML('<div>Showing image ' . $img->filename . '</div>');

image.png.916905d03e02045363fab265de0f4fbe.png As in the Screenshot beside, the seond Path(last line) is the right one.

Thank you bernhard, horst and jens - your all commitment is just great!

  • Like 4

Share this post


Link to post
Share on other sites

Hey Guys, I have one more question, in the hope of solving PDF creation problems.

How would you create a download/store -link for a PDF-File from Page using RockPDF/mpdf library on action (may like pages2pdf) ?

 

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 horst
      Hi, on a site I want to disable access to original images and only allow to access thumbnails and watermarked image variations.
      EDIT:
      A good solution for protecting original images can be found a bit down in this thread:
       
      Old content of this initial post:
       
    • By Guy Incognito
      I seem to be running into a repeated fatal error in a fresh PW install version 3.0.148. I can't quite put my finger on the pattern but it seems to be around deleting image fields or removing images from certain image fields.
      This is the trace from the log generated by trying to save a page and delete an image from an image field:
      Fatal Error: Uncaught Error: Cannot access protected property Pageimage::$original in /wire/core/PageimageVariations.php:256 Stack trace: 1. /wire/core/Pageimage.php(1327): PageimageVariations->getInfo() 2. /wire/core/Wire.php(386): Pageimage->___isVariation() 3. /wire/core/WireHooks.php(823): Wire->_callMethod() 4. /wire/core/Wire.php(450): WireHooks->runHooks() 5. /wire/core/Pageimage.php(1369): Wire->__call() 6. /wire/core/Pageimage.php(399): Pageimage->getOriginal() 7. /wire/core/WireData.php(333): Pageimage->get() 8. /wire/core/PageimageVariations.php(256): Pro Line 256 of /wire/core/PageimageVariations.php Earlier in the day I was experimenting with custom fields for images for the first time and kept running into this error, thinking it was me using this feature wrong and not having time to read up I deleted the custom image fields template and went about my business. So now I don't know if I triggered an issue or whether it was never related to the custom image fields in the first place?
      Any ideas?
    • By humanafterall
      I'm using some Custom fields for images: 
      https://processwire.com/blog/posts/pw-3.0.142/#custom-fields-for-files-images

      When I save the page, and return the fields are blank. When I re-add the text to the fields and save again then the fields save as expected.

      I know this is stated as being quite experimental but it's super useful feature I'd love to get working correctly.
      I have fields that are CKEditor fields but have overidden this on the image specific template. I've also tried it with regular text fields and I get the same bug.
      (currently using Processwire 3.0.155)
      **UPDATE**
      I've found this issue is specific to editing on pages using the PageTable fieldtype. The fields are not saving when I save the page in the PageTable.
    • By Robin S
      Displays image tags overlaid on the thumbnail using customisable colours. This makes it easier to see which images have which tags without needing to open the edit pane for individual images or changing to the list view.
      Screenshot

      Usage
      Enable tags for one or more image fields. Install the Image Thumbnail Tags module. Optionally configure colours for any of your tags.
       
      https://github.com/Toutouwai/ImageThumbnailTags
      https://modules.processwire.com/modules/image-thumbnail-tags/
    • By EyeDentify
      Hello dear PW gurus.

      I have stumbled over a strange error that i all of sudden got when trying to upload an image to a images field on a page.
      There where images allready stored in the field that i wanted to keep, but during the upload the error apear and after that all images are gone from the field and i can´t upload any, i just get the error every time.
      I am running ProcessWire 3.0.153 dev.
      Update:
      After looking in the assets folder i find the folder for the page and the image files seems to be there including the ones i tried to upload when the error occured.
      But they don´t show up in the images field in the page editor.
       
      The error reported:
       
      SQLSTATE[01000]: Warning: 1265 Data truncated for column 'ratio' at row 1 And here is a screenshot of the event:

      The TracyDebugger Error reporting:

      I hope you fine folks could point me in a direction.
      But it seems our old pal set_time_limit() is back.

      Regards, EyeDentify
×
×
  • Create New...