简体   繁体   English

使用Javascript,如何将Google文档转换为PDF文件,然后将其“切片”为多个PDF?

[英]Using Javascript, how can I convert a Google doc to a PDF file then “slice” it into multiple PDFs?

I would like to replicate VBA code that I wrote for Microsoft Word docs using Google docs and Javascript instead. 我想复制我使用Google文档和Javascript为Microsoft Word文档编写的VBA代码。 It would be used in an Angular app that I'm writing. 它会在我正在编写的Angular应用中使用。

The VBA code searches for "slice tags" in the document and generates PDF files when it finds them. VBA代码在文档中搜索“切片标签”,并在找到它们时生成PDF文件。 For example, if a document consists of 15 pages and there are slice tags on pages 4, 7, and 9, the code generates four files: Page1-4.pdf, Page5-7.pdf, Page8-9.pdf, and Page10-15.pdf. 例如,如果文档由15页组成,并且在第4、7和9页上有切片标记,则代码将生成四个文件:Page1-4.pdf,Page5-7.pdf,Page8-9.pdf和Page10 -15.pdf。

In the Javascript implementation, the steps would be: 在Javascript实现中,步骤为:

  1. Convert the Google doc to a PDF file. 将Google文档转换为PDF文件。
  2. "Slice" the PDF file into multiple PDFs by searching the complete PDF for slice tags. 通过在整个PDF中搜索切片标签,将PDF文件“切片”为多个PDF。
  3. Place the sliced PDFs in a specified Google Drive folder. 将切片的PDF放在指定的Google云端硬盘文件夹中。

Note that I asked a similar question here . 请注意,我在这里问了类似的问题。

However, that's a Google Script implementation. 但是,这是Google脚本实现。 What I realized is that in Google Scripts there is no way to address a document on page boundaries. 我意识到,在Google脚本中无法解决页面边界上的文档。 It would be much simpler and cleaner if it did. 如果这样做的话,它将更加简单和整洁。 Instead, I would like to take another approach. 相反,我想采取另一种方法。

Below is Javascript code that partially addresses step 1. It uses this call: 下面是部分解决步骤1的Javascript代码。它使用此调用:

gapi.client.drive.files.export

However, this does NOT create a PDF file. 但是,这不会创建PDF文件。 Rather, it creates a blob of PDF content. 相反,它会创建PDF内容的斑点。 Apparently, the expectation is that you transform the blob into an actual file but the Google APIs do not appear to provide a way to do this. 显然,期望您将Blob转换为实际文件,但Google API似乎没有提供执行此操作的方法。

 async function exportDocument(id, mimeType) { let rtrn; try { await gapi.client.drive.files.export( {'fileId': id, 'mimeType': mimeType, 'fields': 'webViewLink', }) .then(function(resp) { // This returns a content blob NOT an actual file rtrn = resp; // TODO: How to transform the blob into an actual file (eg, PDF) }).catch((err) => { throw new Error(err.message); }); } catch (e) { throw new Error('exportDocument: ' + e.message); } return rtrn; } // end exportDocument 

A Blob is a binary representation of the raw data. Blob是原始数据的二进制表示。

Typically, when working with PDFs, the Blob is turned into a File (File is a Blob), and saved to the local filesystem. 通常,使用PDF时,Blob会变成一个文件(文件是Blob),并保存到本地文件系统中。 For an example, see How to convert Blob to File in JavaScript 有关示例,请参见如何在JavaScript中将Blob转换为File

There is also a 'file-saver' npm library, and, I believe, and Angular/Typescript version as well, if you don't want to do it yourself. 还有一个“文件保存器” npm库,如果您不想自己做的话,我相信还有Angular / Typescript版本。

I haven't done it, but I imagine that if the library that you want to use to manipulate the PDF expects a PDF file, you could probably convert the Blob to a PDF without writing it to the filesystem, and then work from there. 我还没有做,但是我想如果您要用来处理PDF的库期望有一个PDF文件,您可能可以将Blob转换为PDF,而无需将其写入文件系统,然后从那里开始工作。

BTW, your question has one or more 'close' votes, because you asked a very broad question (ie "how do I write my entire application") instead of asking the targeted question that you really needed answered. 顺便说一句,您的问题有一个或多个“接近”票,因为您提出了一个非常广泛的问题(即“我如何编写整个应用程序”),而不是问您真正需要回答的针对性问题。

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

相关问题 有没有办法将 Google 文档分成多个 PDF? - Is there a way to slice a Google doc into multiple PDFs? 将 html 转换为 PDF 并将转换后的 PDF 放入 javascript 中的 zip 文件中 - Convert html into PDF and put the converted PDFs into the zip file in javascript 如何使用Javascript将Google Map转换为PDF? - How to convert Google Map to PDF using Javascript? 使用 Google Drive API v3 将 PDF 文件转换为 Google Doc - Convert a PDF file to Google Doc using Google Drive API v3 如何使用Javascript将HTML / CSS转换为PDF? - How can I convert HTML/CSS to PDF using Javascript? 如何递归访问Google云端硬盘文件夹中的PDF文件并将其全部转换为Google文档? - How do I recursively access PDF files in Google Drive folders and convert them all to Google Doc? 如何将网址提供的文件(pdf / doc)转换为json / string / base64格式,而无需在本地下载? - How can I convert a file (pdf/doc) served at a url into json / string / base64 format without downloading on local? 如何使用javascript将大型json文件拆分为多个小块文件以在谷歌地图中显示 - How can I split large json file into multiple small chunk files using javascript to display in google map JS变量包含PDF或DOC文件。 如何为用户传递此文件? - JS variable contains PDF or DOC File. How I can pass this file for user? 编辑 Google Doc 然后转换为 PDF 的脚本 - Script to edit Google Doc then Convert to PDF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM