You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Google App Scripts合并不同版本PDF求助:生成1.4版PDF或替代方案

Solution for Merging Different Version PDFs in Google Apps Script

Got it, let's break down how to fix your PDF merging issue. You're dealing with two PDFs—one is 1.4 with a trailer, the other is a 1.5 version from Google Docs without a trailer, and your current merge script can't handle the latter. Here are two solid approaches:

Option 1: Export Google Docs as PDF 1.4 Version

The default getAs('application/pdf') method exports Google Docs to PDF 1.5, but you can force a 1.4 export by using Google Docs' direct export URL with the pdfVersion parameter. Here's a script to do that:

function exportGDocToPDF1_4(docId) {
  // Build the export URL with PDF 1.4 specification
  const exportUrl = `https://docs.google.com/document/d/${docId}/export?format=pdf&pdfVersion=1.4`;
  // Get OAuth token for authentication
  const authToken = ScriptApp.getOAuthToken();
  
  // Fetch the PDF blob
  const response = UrlFetchApp.fetch(exportUrl, {
    headers: { 'Authorization': `Bearer ${authToken}` }
  });
  
  // Create the PDF file in Drive
  const pdfBlob = response.getBlob().setName('TEST pdf (1.4)');
  const pdfFile = DriveApp.createFile(pdfBlob);
  
  return pdfFile.getId();
}

Just replace docId with your Google Document ID, and this will generate a PDF 1.4 version with the proper trailer, which should work with your existing merge script.

Option 2: Use PDF-Lib for Robust PDF Merging

If you don't want to mess with PDF versions, PDF-Lib is a powerful JavaScript library that can merge PDFs regardless of their version or trailer status. Here's how to implement it in Apps Script:

Step 1: Add PDF-Lib to Your Project

  1. Go to the PDF-Lib GitHub repo, download the latest pdf-lib.min.js from the dist folder.
  2. In your Apps Script editor, create a new file (click + > Script), name it pdf-lib, and paste the entire contents of pdf-lib.min.js into it.

Step 2: Merging Script

async function mergeTwoPDFs(fileId1, fileId2) {
  // Access PDF-Lib's core functionality
  const { PDFDocument } = PDFLib;

  // Fetch the two PDF files from Drive as blobs
  const pdf1Blob = DriveApp.getFileById(fileId1).getBlob();
  const pdf2Blob = DriveApp.getFileById(fileId2).getBlob();

  // Convert blobs to Uint8Arrays (required by PDF-Lib)
  const pdf1Bytes = new Uint8Array(pdf1Blob.getBytes());
  const pdf2Bytes = new Uint8Array(pdf2Blob.getBytes());

  // Create a new empty PDF document for merging
  const mergedPdfDoc = await PDFDocument.create();

  // Load and copy pages from the first PDF
  const pdf1Doc = await PDFDocument.load(pdf1Bytes);
  const pdf1Pages = await mergedPdfDoc.copyPages(pdf1Doc, pdf1Doc.getPageIndices());
  pdf1Pages.forEach(page => mergedPdfDoc.addPage(page));

  // Load and copy pages from the second PDF
  const pdf2Doc = await PDFDocument.load(pdf2Bytes);
  const pdf2Pages = await mergedPdfDoc.copyPages(pdf2Doc, pdf2Doc.getPageIndices());
  pdf2Pages.forEach(page => mergedPdfDoc.addPage(page));

  // Save the merged PDF as a blob
  const mergedPdfBytes = await mergedPdfDoc.save();
  const mergedBlob = Utilities.newBlob(mergedPdfBytes, 'application/pdf', 'Merged Result.pdf');

  // Create the merged file in Drive and return its ID
  const mergedFile = DriveApp.createFile(mergedBlob);
  return mergedFile.getId();
}

This script parses the structure of each PDF directly, so it doesn't care about trailers or version differences—it just copies all pages into a new merged PDF.

Which Option to Choose?

  • If you need to stick with your existing merge script, go with Option 1 to generate the 1.4 PDF.
  • If you want a more future-proof solution that works with any PDF version, Option 2 with PDF-Lib is better—it handles edge cases way more reliably.

内容的提问来源于stack exchange,提问作者JingleBells

火山引擎 最新活动