title: Google apps script getAs('application/pdf') layout
body: <p>I am generating a PDF file from a spreadsheet using an app script I found <a href="https://stackoverflow.com/q/39690232/8336495">here</a>.
   This one uses the good old
   Method getAs('application/pdf') and works great.</p>
  
  <p>The problem is that the PDF document generated in this way has unwanted asymmetric margins (larger on the right, narrow on the left). I just wanted the page to be centered. The weird thing is that when I print from the Google menu File -> Print (or Ctrl + P) the document appears centered correctly.</p>
  
  <p>My code looks like this:</p>
  
  <pre><code>function CreaPDF() {
    //The function prints an invoice to PDF. First it copies spreadsheet to a new document.
    //Deletes all sheet except the one to print. Saves it to PDF. 
    //It overwrites any existing doc with same name.
  
    var sourceSpreadsheet = SpreadsheetApp.getActive();          
    var sheetName = "Factura";
    var folderID = getParentFolder(); // Folder id to save in a folder.
    var sourceSheet = sourceSpreadsheet.getSheetByName(sheetName);
    var folder = DriveApp.getFolderById(folderID); 
    var numf = sourceSpreadsheet.getRangeByName("NumeroFactura").getValue();
    var anof = numf.split("/",2);   // Seeks number and year -&gt; filename 
  
    var pdfName = anof[1] +"_Factura_" + anof[0]+ "_Dra_Salazar"; // Nombre del documento;
  
    //Copy whole spreadsheet 2 temporary sheet
    var destSpreadsheet = SpreadsheetApp.open(DriveApp.getFileById(sourceSpreadsheet.getId()).makeCopy("tmp_convert_to_pdf", folder))
  
    //delete redundant sheets
    var sheets = destSpreadsheet.getSheets();
    for (i = 0; i &lt; sheets.length; i++) {
      if (sheets[i].getSheetName() != sheetName){
      destSpreadsheet.deleteSheet(sheets[i]);
      }
    }
  
    //Deletes pdf if already exists
    var files = DriveApp.getFilesByName(pdfName);
    while (files.hasNext()) {
     files.next().setTrashed(true);
     }
    var destSheet = destSpreadsheet.getSheets()[0];
    //repace cell values with text (to avoid broken references)
    var sourceRange = sourceSheet.getRange(1, 1,sourceSheet.getMaxRows(),sourceSheet.getMaxColumns());
  
    var sourcevalues = sourceRange.getDisplayValues();
    var destRange = destSheet.getRange(1, 1, destSheet.getMaxRows(), destSheet.getMaxColumns());
    destRange.setValues(sourcevalues);
  
    SpreadsheetApp.getActiveSpreadsheet().toast('Creando PDF');
  
    //save to pdf
    var theBlob = destSpreadsheet.getBlob().getAs('application/pdf').setName(pdfName);
    var newFile = folder.createFile(theBlob);
  
    //Delete the temporary sheet
    DriveApp.getFileById(destSpreadsheet.getId()).setTrashed(true);
    return true;
  }
  </code></pre>
  
  <p>My settings when I print are:</p>
  
  <ul>
  <li>Paper size: "A4"</li>
  <li>Scale: "Normal" (I also tried: "fit to width" and
  "fit on page")</li>
  <li>Orientation: "Portrait"</li>
  <li>Margins: "Normal"</li>
  </ul>
  
  <p>As seen in the <a href="https://productforums.google.com/forum/#!topic/docs/sMtzk-qnThs" rel="noreferrer">Google Help Forums</a> it seems like an old issue with the way google prints. In summary, it seems that print settings are not saved and there is no way to pass any parameter to the getAs('application/pdf') method either. So i assume that the method (and menu print options) use default parameters that can not be modified. Any solution for this? The "Print or change page setup" help page does not help too much.</p>
  
  <p>Thank you very much</p>