简体   繁体   English

Google 应用脚本 PDF 适合页面

[英]Google apps script PDF fit to page

I'm using a script to create a PDF-file of a sheet and save it.我正在使用脚本来创建工作表的 PDF 文件并保存它。 Everything works great except I can't figure out how to make it "fit whole page".一切都很好,除了我不知道如何使它“适合整个页面”。 The code below is for "fit to width" (fitw=true) which is described numerous places – but I can't find how to fit to page instead.下面的代码是针对“适合宽度”(fitw=true)的,它在很多地方都有描述——但我找不到如何适应页面。

 function savePdf(spreadsheetId, sheetName, pdfName, pdfFolderId) { var sheetId = spreadsheetId.getSheetByName(sheetName).getSheetId(); var url_base = spreadsheetId.getUrl().replace(/edit$/,''); var url_ext = 'export?exportFormat=pdf&format=pdf' + '&gid=' + sheetId // following parameters are optional... + '&range=D4:AX74' + '&size=A4' // paper size: legal / letter / A4 + '&portrait=false' // orientation, false for landscape + '&fitw=true' // fit to width, false for actual size + '&top_margin=0.50' + '&bottom_margin=0.50' + '&left_margin=0.50' + '&right_margin=0.50' + '&sheetnames=false' // + '&printtitle=false' + '&pagenumbers=false' //hide optional headers and footers + '&gridlines=false' // hide gridlines + '&fzr=true'; // false = do not repeat row headers (frozen rows) on each page var url_options = {headers: {'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(),}}; var response = UrlFetchApp.fetch(url_base + url_ext, url_options); var blob = response.getBlob().getAs('application/pdf').setName(pdfName + '.pdf'); var folder = DriveApp.getFolderById(pdfFolderId); folder.createFile(blob); }

I've found a solution!我找到了解决方案! It's to add + '&scale=' + scale就是加 + '&scale=' + scale
// 1 = Normal 100% -- 2 = Fit to width -- 3 = Fit to height -- 4 = Fit to Page // 1 = 正常 100% -- 2 = 适合宽度 -- 3 = 适合高度 -- 4 = 适合页面

The fitw parameter should work as expected, but I found a reported issue and I can also experience this problem. fitw参数应该按预期工作,但我发现了一个报告的问题,我也可以遇到这个问题。 Using or not using it does not make any difference.使用或不使用它没有任何区别。

You can click on the star next to the issue number to receive updates and to give more priority to the bug.您可以单击问题编号旁边的星号以接收更新并为该错误提供更多优先级。

This should do it:这应该这样做:

&horizontal_alignment=CENTER &horizo​​ntal_alignment=居中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM