1

I have an old script that (among other things) converts a google document to pdf. It used to work ok, but now two extra blank pages appear in the pdf version of the file.

I just discovered that this problem affects also the "download as pdf" menu option in google documents. There is a number of workarounds in that case, but I need a workaround for google-apps-script.

In this post the solution to a similar problem seems to involve a fine tuning of the page size. I tried something like that, but it does not trivially apply. I also tried some other (kind of random) variations for the page size and margins, but to no avail.

Below I'm pasting a minimal working example. It should create a document file "test" and its pdf version "test.pdf" in your main drive folder.

Any help getting rid of the two extra pages is greatly appreciated.

Thanks

function myFunction() {
  // this function 
  // - creates a google document "test", 
  // - writes "this is a test" inside it
  // - saves and closes the document
  // - creates a pdf version of the document, called "test.pdf"
  //
  // the conversion is ok, except two extra blank pages appear in the pdf version.


  // create google document
  var doc = DocumentApp.create('test');
  var docFile = DriveApp.getFileById( doc.getId() );
  // set margins (I need landscape layout)
  // this is an attempt to a solution, inspired by https://stackoverflow.com/questions/18426817/extra-blank-page-when-converting-html-to-pdf
  var body = doc.getBody();
  body.setPageHeight(595.2).setPageWidth(841.8);   
  var mrg = 40; // in points
  body.setMarginTop(mrg).setMarginBottom(mrg);   
  body.setMarginLeft(mrg).setMarginRight(mrg);   
  // write something
  body.appendParagraph('this is a test').setHeading(DocumentApp.ParagraphHeading.HEADING2).setAlignment(DocumentApp.HorizontalAlignment.CENTER);  
  // save and close file
  doc.saveAndClose();
  // convert file to pdf
  var docblob = docFile.getAs('application/pdf');
  // set pdf name
  docblob.setName("test.pdf");
  // save pdf file
  var file = DriveApp.createFile(docblob);

}
PFB
  • 177
  • 2
  • 13
  • PDF created by your script in your question has 2 blank pages. If my understanding is correct, in my environment, PDF file with only one page without the extra pages is created when I run your script. Do I misunderstand your question? – Tanaike Oct 10 '18 at 22:05
  • If I run the above script I get a google document "test" with one single page, like I expect. However, its pdf version "test.pdf" has two extra blank pages (a total of three pages in the pdf). Could this be a system-dependent thing? I'm running my script (and using google docs) in chrome or safari, on a macbook pro with Sierra (mac OS 10.12.6). I'm going to test this on a windows machine tomorrow first thing. – PFB Oct 10 '18 at 22:32
  • In my environment, when I run your script, a document with one page (the title is test) and a PDF file with one page without the blank pages. Are there some settings for running script? I think that because GAS is run at the server side, the local environment doesn't affect to the script. – Tanaike Oct 10 '18 at 22:35
  • I'm not sure what you mean "settings for running script". I am pretty sure I did not change anything. This is a "new" issue, in my opinion. In May (a few months ago) the same script (on the same system) did not produce the extra blank pages... What system are you using? – PFB Oct 10 '18 at 22:38
  • I had confirmed that whether you had done something when you run your script. But from your reply, I could find that you normally run without doing something. So can you give me a time to think of workaround? What meaning of the system? If you want to know about my OS, I'm using windows now. – Tanaike Oct 10 '18 at 22:42
  • Well, if your system produces the expected result and mine doesn't, it seems to be a system-dependent issue... I'm by no means an expert, but that makes sense, since javascript runs locally.... In my case I'm getting the same problem also if I produce the pdf directly from google documents. Also see https://productforums.google.com/forum/#!topic/docs/xlsQWxgdZJc;context-place=topicsearchin/docs/extra$20blank$20pages$20pdf – PFB Oct 10 '18 at 22:49
  • I'm sorry for the inconvenience. How about using this? 1. ``var url = "https://www.googleapis.com/drive/v3/files/" + fileId + "/export?mimeType=application/pdf&access_token=" + ScriptApp.getOAuthToken()`` 2. ``var url = "https://docs.google.com/document/d/" + fileId + "/export?format=pdf&access_token=" + ScriptApp.getOAuthToken()`` There are 2 endpoints. For each endpoint, please create PDF file using ``DriveApp.createFile(UrlFetchApp.fetch(url).getBlob())``. If these were not the direct solution, I'm sorry. In my environment, I could confirm that PDF files without extra pages were created. – Tanaike Oct 10 '18 at 22:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/181657/discussion-between-pfb-and-tanaike). – PFB Oct 11 '18 at 07:35
  • Ok, I think I have a new element. The issue does not seem to be system dependent, as I am experiencing it on different systems with different browsers. However, it does seem to be *account-dependent*. The exact same google document is converted differently depending on whether I'm using my personal google account (no extra pages) or a Google Suite for Education account (two extra pages). – PFB Oct 11 '18 at 14:46
  • I'm really sorry I couldn't help. Drive API can be used without using browser. So I thought that when the API is called from outside, the situation might be changed. – Tanaike Oct 12 '18 at 02:00
  • I'm using the scripts for some things I do on Google Drive and Google Classroom (I'm a teacher). I never used the APIs from outside (although now I'm curious). Right now I'm thinking this problem affects my school account only. Neither my personal google account, nor those of my fellow teachers have this problem. I hope google is going to do something about this. – PFB Oct 12 '18 at 06:38
  • Thank you for your additional information. – Tanaike Oct 12 '18 at 21:48

1 Answers1

1

I found the source of the problem and a solution in this post on the google product forum, dating 8 months back.

The extra pages appear in the pdf if the option in view -> print layout is not checked. I did some further tests, with my accounts and my colleagues'. The results are consistent:

  1. when view -> print layout is not checked two extra pages appear in the pdf version of the document
  2. when view -> print layout is checked the pdf version of the document has the expected number of pages.
  3. this setting affects also the documentApp services in Google Apps Script. That is: the above script produces the expected pdf version only if the "view->print layout" option in Google Documents is checked.

I do not see how this behaviour could be a "feature", so I think it's a bug. By the way "print layout" does not seem to have any visible effect on my documents (other than messing up the pdf version). I'm surprised that after 8 months the bug is still out there.

Number 3 above surprised me, because I did not think that an option set manually in a (any) google document would affect my scripts. I'm currently looking for a way of setting the "print layout" option from inside the script. So far I had no luck with that.

PFB
  • 177
  • 2
  • 13