如何移除Google Sheets导出PDF的「内容创建者:Google Sheets」标识?
移除Google Sheets导出PDF的「内容创建者」标识
问题情况
我用Google Apps Script做了个脚本,能把Google Sheets里指定工作表导出成PDF,命名后发邮件,功能没问题,但Mac上看PDF的「显示简介」时,会显示「内容创建者:Google Sheets」,想把这个标识去掉,别让收件人知道PDF是从Google Sheets生成的。当前脚本代码如下:
function RunFunction() { makePDF(); //it calls myFunction(); at end of makePDF() } function mailPdf(shNum, shRng, pdfName, email, subject, htmlbody) { var ss = SpreadsheetApp.getActiveSpreadsheet(); var ssId = ss.getId(); var shId = shNum ? ss.getSheets()[shNum].getSheetId() : null; var url_base = ss.getUrl().replace(/edit$/, ''); var url_ext = 'export?exportFormat=pdf&format=pdf' //export as pdf + (shId ? ('&gid=' + shId) : ('&id=' + ssId)) + (shRng ? ('&range=' + shRng) : '') // Modified to use the dynamic range + '&format=pdf' + '&size=letter' //A3/A4/A5/B4/B5/letter/tabloid/legal/statement/executive/folio //+ '&portrait=false' //true= Potrait / false= Landscape //+ '&scale=1.1' //1= Normal 100% / 2= Fit to width / 3= Fit to height / 4= Fit to Page + '&top_margin=0.5' //All four margins set to 0.5 inches + '&bottom_margin=0.5' + '&left_margin=0.5' + '&right_margin=0.5' + '&gridlines=false' //true/false //+ '&printnotes=false' //true/false //+ '&pageorder=2' //1= Down, then over / 2= Over, then down //+ '&horizontal_alignment=CENTER' //LEFT/CENTER/RIGHT + '&vertical_alignment=TOP' //TOP/MIDDLE/BOTTOM //+ '&printtitle=false' //true/false //+ '&sheetnames=false' //true/false //+ '&fzr=false' //true/false frozen rows //+ '&fzc=false' //true/false frozen cols //+ '&attachment=false' //true/false var options = { headers: { 'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(), 'muteHttpExceptions': true } } var response = UrlFetchApp.fetch(url_base + url_ext, options); var blob = response.getBlob().setName(pdfName + '.pdf'); if (email) { var mailOptions = { attachments: blob, htmlBody: htmlbody } MailApp.sendEmail( // email + "," + Session.getActiveUser().getEmail() // use this to email self and others email, // use this to only email users requested subject + ' (' + pdfName + ')', 'html content only', mailOptions ); } } function myFunction() { var sheetNameToExport = "PDF"; // Replace this with the name of the sheet to export (e.g., "PDF") var rangeToExport = "A1:F300"; // Range to make pdf var pdfName = "Sheet PDF"; var recipientEmail = "XXXXXXXX@gmail.com"; var emailSubject = "PDF Subject line"; var htmlBodyContent = "This is a test run<br><br> "; // Replace this with the HTML content of the email (can be plain text as well) // Find the index of the sheet by its name var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets(); var sheetIndex = sheets.findIndex(function (sheet) { return sheet.getName() === sheetNameToExport; }); if (sheetIndex !== -1) { // Call the mailPdf function with the provided parameters mailPdf(sheetIndex, rangeToExport, pdfName, recipientEmail, emailSubject, htmlBodyContent); } else { Logger.log("Sheet not found: " + sheetNameToExport); } }
解决办法
Google Sheets导出PDF时会自动把「Google Sheets」写到元数据里,没法直接靠导出URL的参数改掉。得用pdf-lib库来修改PDF的元数据,把创建者信息清空或者改成自定义内容。
第一步:把pdf-lib库加到脚本里
打开Google Apps Script编辑器,点「资源」>「库」,输入库ID:1ReeQ6WO8kKNxoaA_O0XEQ589cIrRvEBA9qcWpNqdOP17i47u6N9M5Xh0,选最新版本,标识符填PDFLib,然后保存。
第二步:修改mailPdf函数
替换原脚本里的mailPdf函数,改成下面的代码,主要加了加载PDF、修改元数据的逻辑:
async function mailPdf(shNum, shRng, pdfName, email, subject, htmlbody) { var ss = SpreadsheetApp.getActiveSpreadsheet(); var ssId = ss.getId(); var shId = shNum ? ss.getSheets()[shNum].getSheetId() : null; var url_base = ss.getUrl().replace(/edit$/, ''); var url_ext = 'export?exportFormat=pdf&format=pdf' //export as pdf + (shId ? ('&gid=' + shId) : ('&id=' + ssId)) + (shRng ? ('&range=' + shRng) : '') // Modified to use the dynamic range + '&format=pdf' + '&size=letter' //A3/A4/A5/B4/B5/letter/tabloid/legal/statement/executive/folio //+ '&portrait=false' //true= Potrait / false= Landscape //+ '&scale=1.1' //1= Normal 100% / 2= Fit to width / 3= Fit to height / 4= Fit to Page + '&top_margin=0.5' //All four margins set to 0.5 inches + '&bottom_margin=0.5' + '&left_margin=0.5' + '&right_margin=0.5' + '&gridlines=false' //true/false //+ '&printnotes=false' //true/false //+ '&pageorder=2' //1= Down, then over / 2= Over, then down //+ '&horizontal_alignment=CENTER' //LEFT/CENTER/RIGHT + '&vertical_alignment=TOP' //TOP/MIDDLE/BOTTOM //+ '&printtitle=false' //true/false //+ '&sheetnames=false' //true/false //+ '&fzr=false' //true/false frozen rows //+ '&fzc=false' //true/false frozen cols //+ '&attachment=false' //true/false var options = { headers: { 'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(), 'muteHttpExceptions': true } } var response = UrlFetchApp.fetch(url_base + url_ext, options); // 加载PDF文件并修改元数据 const pdfBytes = response.getContent(); const pdfDoc = await PDFLib.PDFDocument.load(pdfBytes); // 清空创建者信息,也可以改成你想要的自定义内容,比如pdfDoc.setCreator('你的名称') pdfDoc.setCreator(''); // 可选:清空作者信息 pdfDoc.setAuthor(''); // 保持PDF标题和文件名一致 pdfDoc.setTitle(pdfName); // 保存修改后的PDF const modifiedPdfBytes = await pdfDoc.save(); const blob = Utilities.newBlob(modifiedPdfBytes, 'application/pdf', pdfName + '.pdf'); if (email) { var mailOptions = { attachments: blob, htmlBody: htmlbody } MailApp.sendEmail( email, // 只发送给指定收件人 subject + ' (' + pdfName + ')', 'html content only', mailOptions ); } }
注意点
- 脚本要启用V8运行时:编辑器顶部点「运行」>「启用新的Apps Script运行时」
- 如果不想用库,也可以直接通过UrlFetchApp拉取pdf-lib的代码加载,但用库更稳定
内容的提问来源于stack exchange,提问作者John Velella
相关产品推荐
相关产品推荐

