user3352437
user3352437

Reputation:

Create PDF from a string in Google Apps Script

am trying to create a PDF in Google Drive from a String I have previously constructed.

var oPdfCreator = {
    hFolderHandle: DocsList.getFolderById('0B1rbQnVtcj5CNWNmekFWMG9DZzA'),
    hPdfHandle: "",

    mToastFeedback: function () {
      SpreadsheetApp.getActiveSpreadsheet().toast('Finished');
    }
};

oPdfCreator.hPdfHandle = oPdfCreator.hFolderHandle.createFile('test.pdf',   Utilities.newBlob(oInvoice.sInvoiceBody).getBytes(), 'application/pdf');

This does not work, the PDF is malformed.

Upvotes: 0

Views: 1985

Answers (2)

M--
M--

Reputation: 28826

The answer above put me on the right track. I hoped to be able to directly create a blob with the PDF content, but I realized this is much easier and readable.

var oPdfCreator = {
  hFolderHandle: DocsList.getFolderById('0B1rbQnVtcj5CNWNmekFWMG9DZzA'),
  hTempHtmlHandle: "",
  hPdfHandle: "",

  mToastFeedback: function () {
      SpreadsheetApp.getActiveSpreadsheet().toast('Finished');
  }
};
oPdfCreator.hTempHtmlHandle = oPdfCreator.hFolderHandle.createFile('test.html',  oInvoice.sInvoiceBody, 'text/html');
oPdfCreator.hPdfHandle = oPdfCreator.hFolderHandle.createFile(oPdfCreator.hTempHtmlHandle.getAs('application/pdf')).rename('test.pdf');
DocsList.getFileById(oPdfCreator.hTempHtmlHandle.getId()).setTrashed(true);

function fProcessRequest () {
  oInvoice.mProcessInvoice();
  oPdfCreator.mToastFeedback();
}

Upvotes: 0

Serge insas
Serge insas

Reputation: 46794

I tried 2 approaches to get the result you want, one didn't work and the other succeeded but I'm not sure it will work for your specific use case.

Give it a try and tell us what happens. In both functions I create a temporary file that I delete on the fly when conversion succeeds.

The first function uses DriveApp only and tries to create an intermediate plain text file but the final conversion fails :

function test1() {
  var pdfContent = 'test pdf content\n\nThis is a normal text in a pdf file';
  var intermediate = DriveApp.createFile('tempFile',pdfContent,MimeType.PLAIN_TEXT);
  var blob = intermediate.getAs(MimeType.PDF);
  Logger.log(blob.getContentType());
  var pdfFile = DriveApp.createFile(blob);
  DriveApp.getFileById(intermediate.getId()).setTrashed(true);
}

The second function assumes the file content is "pure text" and uses DocumentApp to create the intermediate file... this conversion (using DriveApp again) works and I get a valid pdf file.

function test2() {
  var pdfContent = 'test pdf content\n\nThis is a normal text in a pdf file';
  var intermediate = DocumentApp.create('otherTest');
  intermediate.getBody().editAsText().appendText(pdfContent);
  intermediate.saveAndClose();
  var id = intermediate.getId();
  var blob = DriveApp.getFileById(id).getAs(MimeType.PDF);
  Logger.log(blob.getContentType());
  var pdfFile = DriveApp.createFile(blob);
  DriveApp.getFileById(id).setTrashed(true);
}

Upvotes: 1

Related Questions