0

I'm setting up a spreadsheet which creates a pdf file and share it with someone to print it and sign it.

 var content = getMessage(X,Y,Z, etc);
 var FolderFile = DriveApp.getFoldersByName('CreatingFolderTest').next();
 var FileCreation = FolderFile.createFile(EmployeeID, content, MimeType.PDF).addViewer(Email);

the issue is that when I'm trying to download the file it gives me an error that pdf file is not properly decoded.

Tried to search for solutions and found that it might the metadata tags I'm using

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="width=device-width"/>

I expect that I can create a file in PDF format from the HTML file already in the code and send it to the employee to print it out! will be extra if I can add a password to the pdf file, but that's a kind of extra mile

basel adel
  • 35
  • 1
  • 5
  • Can I ask you about the mimeType of ``content`` and ``FileCreation``? – Tanaike Jun 17 '19 at 22:24
  • Content is a getMessage function which returns html output – basel adel Jun 18 '19 at 06:29
  • Thank you for replying. I thought that I could understand your question. So I proposed a modified script as an answer. Could you please confirm it? I'm not sure about your whole HTML. So please test the modified script for your HTML data. If the modified script didn't work for your HTML. I apologize. – Tanaike Jun 18 '19 at 07:36
  • The modification works perfect, thanks, man! – basel adel Jun 18 '19 at 11:29

1 Answers1

1
  • You want to create the file of text/html as a PDF file.
  • MimeType of content of var content = getMessage(X,Y,Z, etc) is text/html.

If my understanding is correct, how about this modification?

Modification points:

  • Unfortunately, the file of text/html cannot be directly converted to PDF format using createFile(EmployeeID, content, MimeType.PDF). When your script is run, a file that the content and mimeType are HTML and application/pdf is created. By this, when the file is opened, an error occurs. Because the content is not PDF format.
  • In order to avoid this issue, I used HtmlService.createHtmlOutput().

Please think of this as just one of several answers.

Modified script:

From:
var content = getMessage(X,Y,Z, etc);
var FolderFile = DriveApp.getFoldersByName('CreatingFolderTest').next();
var FileCreation = FolderFile.createFile(EmployeeID, content, MimeType.PDF).addViewer(Email);
To:
var content = getMessage(X,Y,Z, etc);
var FolderFile = DriveApp.getFoldersByName('CreatingFolderTest').next();

var html = HtmlService.createHtmlOutput(content); // Added
var FileCreation = FolderFile.createFile(html.getAs(MimeType.PDF)).setName(EmployeeID); // Modified

Note:

  • If the HTML includes the images retrieving from URLs, please put the images to HTML as base64 data. You can see the sample script at here.

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thanks man, this one works perfectly! Sorry for asking but any idea on the password protection? – basel adel Jun 18 '19 at 11:28
  • This is using the file type converter built into Google Drive, which doesn't have options for adding passwords. – Jeremy Jun 18 '19 at 12:53
  • @basel adel Thank you for replying. I'm glad your issue was resolved. About your another question, unfortunately, in the current stage, the password cannot be set to PDF using ``getAs(MimeType.PDF)`` and Google APIs. I apologize for this situation. So as one of workarounds, for example, how about looking for the external API for achieving it? – Tanaike Jun 18 '19 at 22:26
  • @Jeremy Thank you for your support. – Tanaike Jun 18 '19 at 22:26