合并两个PDF文件时出现超时错误的函数。

huangapple go评论68阅读模式
英文:

Function to Combine 2 PDFs getting timeout error

问题

这是我的用于将两个PDF合并成一个的函数(这是在一个较大的forEach循环中进行的,但我没有包括它,因为我不想让问题变得太复杂)。

我已经注意到了所有的控制台日志输出,它们似乎表明PDF文件是有效的,并且成功加载了。然而,函数的最终结果是创建一个名为"combined.pdf"的新文件,但这并没有发生。在运行我的函数后,我收到了以下错误消息:"ReferenceError: setTimeout is not defined"

    // ****** FUNCTION 1 ******
    async function loadPDF(pdfBytes) {
      try {
        const loadedPdf = await PDFLib.PDFDocument.load(pdfBytes);
        console.log(`PDF loaded successfully`);
        return loadedPdf;
      } catch (error) {
        console.log(`Error loading PDF:`, error);
        return null;
      }
    }

    // ****** MAIN FUNCTION ******
    async function combinePDFsIntoOne () {

      const cdnjs = 'https://cdn.jsdelivr.net/npm/pdf-lib@1.16.0/dist/pdf-lib.min.js';
      eval(UrlFetchApp.fetch(cdnjs).getContentText());

      const pdfFile1 = pdf;
      const pdfFile2 = pdfP2FileName;

      console.log(typeof pdfFile1) //object
      console.log(pdfFile1)
      /*
      { toString: [Function],
      getName: [Function],
      getBytes: [Function],
      setName: [Function],
      getContentType: [Function],
      setContentType: [Function],
      setBytes: [Function],
      getDataAsString: [Function],
      setDataFromString: [Function],
      isGoogleType: [Function],
      getAllBlobs: [Function],
      setContentTypeFromExtension: [Function],
      copyBlob: [Function],
      getAs: [Function] }
      */

      console.log(typeof pdfFile2) //object
      console.log(pdfFile2)
      /*
      { toString: [Function],
      getName: [Function],
      getBytes: [Function],
      setName: [Function],
      getContentType: [Function],
      setContentType: [Function],
      setBytes: [Function],
      getDataAsString: [Function],
      setDataFromString: [Function],
      isGoogleType: [Function],
      getAllBlobs: [Function],
      setContentTypeFromExtension: [Function],
      copyBlob: [Function],
      getAs: [Function] }
      */

      // 以“二进制字符串”的形式读取文件
      const pdfBytes1 = new Uint8Array(pdfFile1.getBytes()); 
      const pdfBytes2 = new Uint8Array(pdfFile2.getBytes()); 

      // 创建新的PDF文档
      const pdfDoc = await PDFLib.PDFDocument.create();

      // // 加载第一个和第二个PDF文档
      // const firstPDF = await PDFLib.PDFDocument.load(pdfBytes1);
      // const secondPDF = await PDFLib.PDFDocument.load(pdfBytes2);

      // 尝试加载第一个PDF
      const firstPDF = await loadPDF(pdfBytes1); // PDF加载成功
      console.log(typeof firstPDF);
      if (!firstPDF) {
        console.log(`Error loading first PDF ${typeof firstPDF}`);
      }
      // 尝试加载第二个PDF
      const secondPDF = await loadPDF(pdfBytes2); // PDF加载成功
      if (!secondPDF) {
        console.log(`Error loading second PDF ${typeof secondPDF}`);
      }

      // 获取要导入的页面
      const firstPDFPages = await pdfDoc.copyPages(firstPDF, firstPDF.getPageIndices());
      const secondPDFPages = await pdfDoc.copyPages(secondPDF, secondPDF.getPageIndices());

      // 向PDF中添加页面
      firstPDFPages.forEach((page) => pdfDoc.addPage(page));
      secondPDFPages.forEach((page) => pdfDoc.addPage(page));

      const pdfBytes = await pdfDoc.save();
      const newBlob = Utilities.newBlob(Array.from(pdfBytes), 'application/pdf');

      newBlob.setName('combined.pdf');
      holdingFolder.createFile(newBlob);

    };

    try {
      await combinePDFsIntoOne ();
    }
    catch(err) {
      console.log(`Error is: ${err}`) // Error is: ReferenceError: setTimeout is not defined
    }
英文:

Here is my function that I'm using to combine two PDFs into one (this is happening within a larger forEach loop but I didn't include it because I don't want to overcomplicate my question).

I have noted all the console log outputs, which seem to indicate the PDF files are valid and they load successfully. However, the end result of the function is to create a new file called "combined.pdf" and this is not happening. I am getting this error after I run my function: "ReferenceError: setTimeout is not defined"

    // ****** FUNCTION 1 ******
async function loadPDF(pdfBytes) {
try {
const loadedPdf = await PDFLib.PDFDocument.load(pdfBytes);
console.log(`PDF loaded successfully`);
return loadedPdf;
} catch (error) {
console.log(`Error loading PDF:`, error);
return null;
}
}
// ****** MAIN FUNCTION ******
async function combinePDFsIntoOne () {
const cdnjs = 'https://cdn.jsdelivr.net/npm/pdf-lib@1.16.0/dist/pdf-lib.min.js';
eval(UrlFetchApp.fetch(cdnjs).getContentText());
const pdfFile1 = pdf;
const pdfFile2 = pdfP2FileName;
console.log(typeof pdfFile1) //object
console.log(pdfFile1)
/*
{ toString: [Function],
getName: [Function],
getBytes: [Function],
setName: [Function],
getContentType: [Function],
setContentType: [Function],
setBytes: [Function],
getDataAsString: [Function],
setDataFromString: [Function],
isGoogleType: [Function],
getAllBlobs: [Function],
setContentTypeFromExtension: [Function],
copyBlob: [Function],
getAs: [Function] }
*/
console.log(typeof pdfFile2) //object
console.log(pdfFile2)
/*
{ toString: [Function],
getName: [Function],
getBytes: [Function],
setName: [Function],
getContentType: [Function],
setContentType: [Function],
setBytes: [Function],
getDataAsString: [Function],
setDataFromString: [Function],
isGoogleType: [Function],
getAllBlobs: [Function],
setContentTypeFromExtension: [Function],
copyBlob: [Function],
getAs: [Function] }
*/
// Read files as "Binary Strings"
const pdfBytes1 = new Uint8Array(pdfFile1.getBytes()); 
const pdfBytes2 = new Uint8Array(pdfFile2.getBytes()); 
// Create new pdf
const pdfDoc = await PDFLib.PDFDocument.create();
// // Load the first and second PDFs
// const firstPDF = await PDFLib.PDFDocument.load(pdfBytes1);
// const secondPDF = await PDFLib.PDFDocument.load(pdfBytes2);
// Try loading the first PDF
const firstPDF = await loadPDF(pdfBytes1); // PDF loaded successfully
console.log(typeof firstPDF);
if (!firstPDF) {
console.log(`Error loading first PDF ${typeof firstPDF}`);
}
// Try loading the second PDF
const secondPDF = await loadPDF(pdfBytes2); // PDF loaded successfully
if (!secondPDF) {
console.log(`Error loading second PDF ${typeof secondPDF}`);
}
// Get pages to import
const firstPDFPages = await pdfDoc.copyPages(firstPDF, firstPDF.getPageIndices());
const secondPDFPages = await pdfDoc.copyPages(secondPDF, secondPDF.getPageIndices());
// Add pages to the PDF
firstPDFPages.forEach((page) => pdfDoc.addPage(page));
secondPDFPages.forEach((page) => pdfDoc.addPage(page));
const pdfBytes = await pdfDoc.save();
const newBlob = Utilities.newBlob(Array.from(pdfBytes), 'application/pdf');
newBlob.setName('combined.pdf');
holdingFolder.createFile(newBlob);
};
try {
await combinePDFsIntoOne ();
}
catch(err) {
console.log(`Error is: ${err}`) // Error is: ReferenceError: setTimeout is not defined
}

答案1

得分: 1

哇,我找到了bug!我只需要定义这个变量就可以了:

const setTimeout = function (f, t) {
  Utilities.sleep(t);
  return f();
}

再次感谢Tanaike,他对Google Apps Script真是一个很好的资源!

英文:

Wow, I found the bug! All I had to do was define this variable:

const setTimeout = function (f, t) {
Utilities.sleep(t);
return f();
}

Thanks again to Tanaike for being such a great resource for Google Apps Script!

huangapple
  • 本文由 发表于 2023年7月28日 02:29:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782525.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定