我想以 pdf 格式下载在谷歌幻灯片中打开的当前页面。 Google 幻灯片允许在他们自己的用户界面中将整个演示文稿下载为 PDF,但我有兴趣一张一张地下载演示文稿中的幻灯片。
现在我可以选择当前页面,
export const getCurrentPage = () => {
return SlidesApp.getActivePresentation()
.getSelection()
.getCurrentPage();
};
此函数返回一个 Page Class。
问题是我不知道如何将这个“页面类”转换为 PDF 并下载它。我已经检查了可用的方法,但还没有找到任何有用的东西。有什么建议吗?
答案 0 :(得分:2)
Save Google Slide as PDF using Google Apps Script 将整个幻灯片文件转换为 PDF 并将其保存到您的 Google Drive。如果您想在本地计算机上下载特定幻灯片,可以参考示例代码。
function onOpen() {
// get the UI for the Spreadsheet
const ui = SlidesApp.getUi();
// add the menu
const menu = ui.createMenu("Download")
.addItem("Download Current Slide",'downloadModal')
.addToUi();
}
function downloadModal() {
var pdfFile = convertToPdf();
//Get the pdf file's download url
var html = "<script>window.open('" + pdfFile.getDownloadUrl() + "');google.script.host.close();</script>";
var userInterface = HtmlService.createHtmlOutput(html)
.setHeight(10)
.setWidth(100);
SlidesApp.getUi().showModalDialog(userInterface, 'Downloading PDF ... ');
//Delete pdf file in the drive
pdfFile.setTrashed(true);
}
function convertToPdf() {
//Get current slide
var presentation = SlidesApp.getActivePresentation();
var slide = presentation.getSelection().getCurrentPage().asSlide();
Logger.log(slide.getObjectId());
//Create temporary slide file
var tmpPresentation = SlidesApp.create("tmpFile");
//Delete default initial slide
tmpPresentation.getSlideById("p").remove();
//Add current slide to the temporary presentation
tmpPresentation.insertSlide(0,slide);
tmpPresentation.saveAndClose();
//Create a temporary pdf file from the temporary slide file
var tmpFile = DriveApp.getFileById(tmpPresentation.getId());
var pdfFile = DriveApp.createFile(tmpFile.getBlob().setName("slide.pdf"))
//delete temporary slide file
tmpFile.setTrashed(true);
return pdfFile;
}
将当前幻灯片转换为 PDF 文件:
注意:
文件中的第一张幻灯片有一个 object id = 'q'
,您可以在浏览器中查看幻灯片 url 中的对象 ID#slide=id.<object id>
下载文件:
downloadModal()
将当前幻灯片下载到本地计算机中convertToPdf()
以获取 pdf 文件的 File 对象。使用 File.getDownloadUrl()