如何调整PDF上的页边距,我需要将PDF设置为适合页面

问题描述 投票:0回答:1

我的基本脚本运行良好。它完全按照我想要的方式做。它需要 Google Sheet,将其转换为 PDF,然后通过电子邮件将 PDF 发送给我。

我的问题是,如何调整 PDF 的页边距?我需要设置 PDF 以适合页面。我不能只调整纸张大小,因为它会消除间距。

    /* Email Google Spreadsheet as PDF */
function PDF() {
  
  // Send the PDF of the spreadsheet to this email address
  var email = "gmail.com"; 
  
  // Get the currently active spreadsheet URL (link)
  var ss = SpreadsheetApp.openByUrl(
     'https://docs.google.com');

  // Subject of email message
  var subject = "PAR - " + ss.getRange("A6:A6").getValue() +" - "+ ss.getRange("A5:A5").getValue(); 

  // Email Body can  be HTML too 
  var body = "Name -  " + ss.getRange("A6:A6").getValue() +" - "+ ss.getRange("A5:A5").getValue();
  
  var blob = DriveApp.getFileById(ss.getId()).getAs("application/pdf");
  
  blob.setName("Name - " + ss.getRange("A6:A6").getValue() +" - "+ ss.getRange("A5:A5").getValue() + ".pdf");
  
  // If allowed to send emails, send the email with the PDF attachment
  if (MailApp.getRemainingDailyQuota() > 0) 
    GmailApp.sendEmail(email, subject, body, {
      htmlBody: body,
      attachments:[blob]
    });
}

我见过这样的脚本,但不知道如何让它在我的脚本上运行。

var url_ext = 'exportFormat=pdf&format=pdf'        // export as pdf / csv / xls / xlsx
  + '&size=letter'                       // paper size legal / letter / A4
  + '&portrait=false'                    // orientation, false for landscape
  + '&fitw=true&source=labnol'           // fit to page width, false for actual size
  + '&sheetnames=false&printtitle=false' // hide optional headers and footers
  + '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
  + '&fzr=false'                         // do not repeat row headers (frozen rows) on each page
  + '&gid=';                             // the sheet's Id
google-apps-script google-sheets pdf pdf-generation
1个回答
1
投票

我也有同样的问题,我也想删除边距。这是有帮助的,这是我的工作脚本,其中包括您上面提到的部分。 但我没有看到可以调整边距的参数...

function CreaPDF() {

  var report = SpreadsheetApp.getActive();            
  var pdfName = "ReportXXX";
  var sheetName = "Sheet1";
  var sourceSheet = report.getSheetByName(sheetName);

  SpreadsheetApp.getActiveSpreadsheet().toast('Creating the PDF');

  // export url
  var url = 'https://docs.google.com/spreadsheets/d/'+report.getId()+'/export?exportFormat=pdf&format=pdf' // export as pdf / csv / xls / xlsx
  + '&size=A4'                           // paper size legal / letter / A4
  + '&portrait=false'                     // orientation, false for landscape
  + '&fitw=true'                        // fit to page width, false for actual size
  + '&sheetnames=false&printtitle=false' // hide optional headers and footers
  + '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
  + '&fzr=false'                         // do not repeat row headers (frozen rows) on each page
  + '&gid='+sourceSheet.getSheetId();    // the sheet's Id

  var token = ScriptApp.getOAuthToken();

  // request export url
  var response = UrlFetchApp.fetch(url, {
    headers: {
      'Authorization': 'Bearer ' +  token
    }
  });

  var theBlob = response.getBlob().setName(pdfName+'.pdf');
  
  //var attach = {fileName:'Monthly Report.pdf',content:pdf, mimeType:'application/pdf'};
  
  var name = report.getRange("H1:H1").getValues();   // Get Name
  var emailTo = report.getRange("H2:H2").getValues();   // Get email
  var period = report.getRange("H3:H3").getValues();   // Get Reporting Period
  var subject = " - TEST Monthly Report - " + period;   // Construct the Subject Line
  var message = "Hi " + name + ", here is your latest report for " + period; // email body text

  // Send the freshly constructed email 
  MailApp.sendEmail(emailTo, subject, message, {attachments:[theBlob]}); 


}

© www.soinside.com 2019 - 2024. All rights reserved.