You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Context: I have a function that gets an array of google drive's files' ids (PDFs), merge them and save the merged PDF on the same folder. This function is running in a google form, using google apps script.
Here is the function:
async function mergeAllPDFs(ids,pasta) {
//checa a quantidade de PDFs
const numDocs = ids.length;
console.log("numDocs:"+numDocs);
//cria o PDF onde será escrito os dados dos PDFs enviados
const pdfDoc = await PDFLib.PDFDocument.create().catch((err) => { console.error("Erro ao criar PDF final: "+err); });
console.log("pdfDoc criado ("+pdfDoc+")");
//varre a lista de PDFs enviados, escrevendo os dados no novo PDF
for(var i = 0; i < numDocs; i++) {
console.log("ID do "+i+"º PDF:"+ids[i]);
//Codifica os bytes do PDF em base64 para ser usado pela biblioteca PDF-lib
const donorPdfBytes = Utilities.base64Encode(DriveApp.getFileById(ids[i]).getBlob().getBytes());
console.log("tamanho do array de bytes64 do "+i+"º PDF: "+donorPdfBytes.length);
//carrega o PDF enviado no módulo da Biblioteca PDF-lib
const donorPdfDoc = await PDFLib.PDFDocument.load(donorPdfBytes).catch((err) => { console.error("Erro ao carregar PDF no módulo da lib: "+err); });
console.log("donorPdfDoc: " +donorPdfDoc);
//checa a quantidade de páginas do PDF
const docLength = donorPdfDoc.getPageCount();
console.log("quantidade de páginas: "+docLength);
//varre as páginas copiando-as no novo PDF
for(var k = 0; k < docLength; k++) {
const [donorPage] = await pdfDoc.copyPages(donorPdfDoc, [k]).catch((err) => { console.error("Erro ao copiar páginas: "+err); });
console.log("Doc " + i+ ", page " + k);
pdfDoc.addPage(donorPage);
console.log("página adicionada");
}
}
//Salva o novo PDF
const base64Bytes = await pdfDoc.saveAsBase64().catch((err) => { console.error("Erro ao salvar dados do pdf: "+err); });
console.log("criando arquivo no drive");
//decodifica o PDF para salvar no drive
var data = Utilities.base64Decode(base64Bytes);
//cria o blob (nome que o google appscript da para os dados de um arquivo)
var blob = Utilities.newBlob(data).setName(mesclado).setContentType("application/pdf")
//Cria um novo pdf a partir do blob na pasta dada
pasta.createFile(blob);
console.log("arquivo criado");
}
when I run with 8 or less PDFs, it sucessfully completes.
with more than that, it gives an error: ReferenceError: setTimeout is not defined
at this line: const base64Bytes = await pdfDoc.saveAsBase64().catch((err) => { console.error("Erro ao salvar dados do pdf: "+err);});
Any insight on this? Thanks in advance!
(and sorry for the code comments in Portuguese)
Edit: I used err.name/message/stack to to get more details and it gives me this:
Name: ReferenceError, Message: setTimeout is not defined, Stack:ReferenceError: setTimeout is not defined
at eval (eval at <anonymous> (Código:3:1), <anonymous>:15:6606)
at new Promise (<anonymous>)
at X (eval at <anonymous> (Código:3:1), <anonymous>:15:6581)
at eval (eval at <anonymous> (Código:3:1), <anonymous>:15:265856)
at eval (eval at <anonymous> (Código:3:1), <anonymous>:15:1845)
at Object.eval [as next] (eval at <anonymous> (Código:3:1), <anonymous>:15:1950)
at a (eval at <anonymous> (Código:3:1), <anonymous>:15:689)
Line 3 is me calling: eval(UrlFetchApp.fetch('https://cdn.jsdelivr.net/npm/pdf-lib/dist/pdf-lib.min.js').getContentText());
Edit2: I copied the whole lib to get a better error output, instead of using eval. And this is the error:
Name: ReferenceError, Message: setTimeout is not defined, Stack:ReferenceError: setTimeout is not defined
at pdf-lib:460:13
at new Promise (<anonymous>)
at waitForTick (pdf-lib:459:16)
at PDFStreamWriter.<anonymous> (pdf-lib:17030:50)
at step (pdf-lib:91:27)
at Object.next (pdf-lib:72:57)
at fulfilled (pdf-lib:62:62)
Edit3: I fixed it. Just for future reference, for people using GAS and have the same problem:
I changed the return on line 460:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Context: I have a function that gets an array of google drive's files' ids (PDFs), merge them and save the merged PDF on the same folder. This function is running in a google form, using google apps script.
Here is the function:
when I run with 8 or less PDFs, it sucessfully completes.
with more than that, it gives an error: ReferenceError: setTimeout is not defined
at this line:
const base64Bytes = await pdfDoc.saveAsBase64().catch((err) => { console.error("Erro ao salvar dados do pdf: "+err);});
Any insight on this? Thanks in advance!
(and sorry for the code comments in Portuguese)
Edit: I used err.name/message/stack to to get more details and it gives me this:
Line 3 is me calling:
eval(UrlFetchApp.fetch('https://cdn.jsdelivr.net/npm/pdf-lib/dist/pdf-lib.min.js').getContentText());
Edit2: I copied the whole lib to get a better error output, instead of using eval. And this is the error:
Edit3: I fixed it. Just for future reference, for people using GAS and have the same problem:
I changed the return on line 460:
to this, using Utilities.sleep instead of setTimeout:
Beta Was this translation helpful? Give feedback.
All reactions