Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
pdf-lib drawImage no funciona en nodejs incluso después de manejar con promesa

Tengo una variedad de contenidos para imprimir en un pdf que incluye imágenes y texto. la muestra es

 boxes=[{type: 'text', value: 'sample'}, {type: 'image', url: 'someurl'}]

Estoy usando forEach para iterar a través de la matriz y dibujarTexto o dibujarImagen. drawText funciona bien con todas las páginas. pero drawImage no funciona o la imagen no se ve en el resultado final.

el código es como

 boxes.forEach(async (box, i) => { const pageNo = parseInt(box.pageNo) - 1; const page = pages[pageNo]; // Get the width and height of the first page const { width, height } = page.getSize(); let drawTextString = box.value; let isImage = false; let imageUrl = ""; const image = async (url, box) => { return new Promise(async (resolve, reject) => { const pngImageBytes = await fetch(url).then((res) => res.arrayBuffer()); const pngImage = await pdfDoc.embedPng(pngImageBytes); page.drawImage(pngImage, { x: (width * box.left) / 100 + 5 + leftOffset, y: height - (height * box.top) / 100 - 15 - bottomOffset, width: pngImage.width, height: pngImage.height, }); console.log("done"); if (i === relevantBoxes.length - 1) writePdf(); resolve(); }); }; switch (box.field) { case "image": if (box.signImage) { isImage = true; imageUrl = box.signImage; } break; case "text": break; default: break; } if (!isImage) { page.drawText(drawTextString, { x: (width * box.left) / 100 + 5 + leftOffset, y: height - (height * box.top) / 100 - 15 - bottomOffset, size: parseInt(box.editorValues.fontSize.replace("px", "")), font, color: rgb(0, 0, 0), }); if (i === relevantBoxes.length - 1) writePdf(); } else { await image(imageUrl, box); } }); async function writePdf() { const pdfBytes = await pdfDoc.save(); }

Pensé que era por un comportamiento asincrónico. pero el flujo de código parece estar bien.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!