0
votes

I have a document that i use as a template, url: https://docs.google.com/document/d/1Ng00Sw0V-3_htLF2-SyPj895g_V9bi1mKQc1LOaHPNY/edit?usp=sharing

I'm using a script when a form is submited and export it to pdf

function testExport() {
   var pdf = DocumentApp.openById(docTemplate).getAs("application/pdf");
  DriveApp.createFile(pdf);
};

But the exported link looks like https://drive.google.com/file/d/0B_YrT5Ue-LAvUllyQ0ZpbkRodVU/view?usp=sharing

Size of rows between the table seems to increased and the overall quality looks bad, is there a way to fix it? When I download the doc file as pdf the then it looks really good.

1
I can't see in your example about changing row size, but I can see that automatic pagination is not consistent between docs and apps script PDF interface.rpm
What do you mean by that. In my document fontsize between tables is set to 6 but in the pdf it changes to 10 or 12 and then the document overflows. It only happens when exporting with script if I download as pdf it looks fine.sinofis

1 Answers

1
votes

Document.getAs() uses a slightly different converter then the Google Docs UI does. You can get closer by using the conversion functionality built into the Drive API, exposed in File.exportLinks. The sample below uses the Drive Advanced Service to do the conversion and save the result.

function exportAsPdf(documentId) {
  var file = Drive.Files.get(documentId);
  var url = file.exportLinks['application/pdf'];
  var token = ScriptApp.getOAuthToken();
  var response = UrlFetchApp.fetch(url, {
    headers: {
      'Authorization': 'Bearer ' +  token
    }
  });
  var contents = response.getBlob();
  contents.setName(file.title + '.pdf');
  DriveApp.createFile(contents);
}