0

I am using templated html and HtmlService in Google Apps Scripts to generate pdf reports. I am trying to insert an image from my Google Drive into the html template.

The templated html looks like this:

<img src= <?= DriveApp.getFileById("1234567890-abcdefghijklmnopqrstuvwxyz").getAs("image/jpeg");?> >

and the output html looks like this:

<img src="Blob">

If I remove the .getAs() function then the output is:

<img src="image name.jpg"> 

Both snippets of code only result in a hollow, square box only a few pixels wide when the html is evaluated. Is there any way that the image can be inserted into the html instead of a Blob or File objects?

SashaZd
  • 3,315
  • 1
  • 26
  • 48

2 Answers2

-1

It's because the IMG tag cannot use a blob as a source, use

<img src= <?= DriveApp.getFileById(id).getDownloadUrl();?> >

Chek the documentation getDowloadUrl()

  • Doesn't seem to have had any effect. Does seem like it should work, though. – Ian Dillingham Mar 02 '17 at 20:12
  • 1
    Is you imager shared with anyone with the link? – Rubén Mar 02 '17 at 23:55
  • Is it giving you anything before transforming it to PDF, in the HTML result? Share your code so we can help you out – Juan Diego Antezana Mar 03 '17 at 08:22
  • The download link appears in the `` tag, which when plugged into a new browser tab shows the image. But if I try to open the link in a tab that's not using my work email, it won't show up. Perhaps because of the sharing settings the Google servers are not able to access the image? I have the sharing settings set to "Anyone at [my domain] can find and edit" (link sharing is on), which is the most open sharing setting that I can give it. – Ian Dillingham Mar 03 '17 at 16:12
  • 1
    Check this answer: https://stackoverflow.com/questions/43941270/create-pdf-from-html-in-google-apps-script-and-include-images-images-not-showi — looks like the thing is about images not getting rendered when you convert an html file into pdf, workaround is to insert images encoded into base64. – a-change Jan 16 '18 at 23:18
-2

Regarding to put an image into HTML in google apps script you should:

  1. Upload to google drive
  2. Get the ID of the image
  3. Then put in in the format below
img src="https://drive.google.com/uc?export=view&id=1dPKfyZlZFZzauqA8DcaB3h6oHX7sPqs8" width="150px" height="200px" border="5"

1dPKfyZlZFZzauqA8DcaB3h6oHX7sPqs8 is the ID of the picture.

wscourge
  • 10,657
  • 14
  • 59
  • 80