PFB
PFB

Reputation: 167

How to get rid of unwanted extra pages when converting a goole document to pdf via google-apps-script?

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);

}

Upvotes: 1

Views: 2382

Answers (1)

PFB
PFB

Reputation: 167

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.

Upvotes: 1

Related Questions