Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't display image in pdf-template? #32

Open
dansarn opened this issue Mar 12, 2018 · 7 comments
Open

Can't display image in pdf-template? #32

dansarn opened this issue Mar 12, 2018 · 7 comments

Comments

@dansarn
Copy link

dansarn commented Mar 12, 2018

No matter how i try i can't get a image to show in the PDF.
Or rather: When i try to include an image my PDF fails to open.
There is a error message saying "Fel, Det gick inte att läsa in PDF-dokumentet.", meaning "Error, unable to open the PDF document.".

Is there still any support for this plugin, cuz i don't think i will be able to solve it by my own. :(

@Julianoe
Copy link

Julianoe commented Mar 12, 2018

Hi @dansarn !
This plugin uses the DomPDF library. As i've been using this library for some time i have had this type of problem.
Do you provide the PDF with the url of the image? You may want to try to provide the absolute path instead.

If you want to include to the template the thumbnail of a post for example instead of just including it like usual you should do something like:

$upload_dir = wp_upload_dir();
$upload_dir_basedir = $upload_dir['basedir'];
$upload_dir_baseurl = $upload_dir['baseurl'];
$the_thumbnail_link_url = str_replace( ['https', 'http'], 'http', get_the_post_thumbnail_url($postID, 'full') );
$the_thumbnail_link_path = str_replace( $upload_dir_baseurl, $upload_dir_basedir, $the_thumbnail_link_url );

@dansarn
Copy link
Author

dansarn commented Mar 12, 2018

Thanks for the reply @Julianoe! Still can't get it to work though. :(
When i use a link to an non-existing image (broken/false link) it works to display the PDF with a "Image not found or type unknown". But as soon as i use a link to a existing image it fails to load the PDF.

@dansarn
Copy link
Author

dansarn commented Mar 12, 2018

(oops accidentally, closed the issue....)
Thank's again @Julianoe, yet still no luck.

<?php

  $upload_dir = wp_upload_dir();
  $upload_dir_basedir = $upload_dir['basedir'];
  $upload_dir_baseurl = $upload_dir['baseurl'];
  $the_thumbnail_link_url = str_replace( Array('https', 'http'), 'http', get_the_post_thumbnail_url(get_the_ID(), 'full') );
  $the_thumbnail_link_path = str_replace( $upload_dir_baseurl, $upload_dir_basedir, $the_thumbnail_link_url );

?>

<div class="post-content">
  <?php echo "<img src='" . $the_thumbnail_link_path . "' />"; ?>
  <?php the_content(); ?>
</div>

This code above lets me display the PDF, but with a "Image not found or type unknown"-error.
skarmavbild 2018-03-12 kl 18 53 55

If i instead use "$the_thumbnail_link_url" as the img src i cant even get the PDF to open.

@dansarn dansarn closed this as completed Mar 12, 2018
@dansarn dansarn reopened this Mar 12, 2018
@Julianoe
Copy link

I don't have the time this evening but i'll try it on my end soon and i'll let you know what can be done maybe. Stay tuned

@dansarn
Copy link
Author

dansarn commented Mar 12, 2018

Oh, sweet @Julianoe! Thanks! I'm all ears! :D

@dansarn
Copy link
Author

dansarn commented Mar 22, 2018

Any success yet, @Julianoe ? :)

@dygiphy
Copy link

dygiphy commented Oct 16, 2018

I use get_the_post_thumbnail_url() inside the loop and it works for me.

Note that the generated PDF links to the image - it does not embed it. This means you must have a live internet connection that can reach the source images for it to work.

If you want to embed an image, you can by base64 encoding it. I use the following code:

<?php 
function base64_encode_image($filename=string) {
  $path = $filename;
  $type = pathinfo($path, PATHINFO_EXTENSION);
  $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
  return $base64;
}

I use this function in the PDF template:


<img src="<?php global $post; echo base64_encode_image(get_the_post_thumbnail_url($post->ID)) ?>">

You can see this in action at:
https://www.metromotion.com.au/project_profiles/bulla-dairy-foods-storage-silo-upgrade/pdf/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants