Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

214
Visualizações
pdf-lib drawImage is not working in nodejs even after handling with promise

I have an array of contents to be printed to a pdf which includes both images and text. sample is

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

I am using forEach to iterate through array and drawText or drawImage. drawText is working fine with all pages. but drawImage is not working or the image is not visible in final result.

code is as

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();
}

I thought it was because asynchronous behaviour. but the code flow seems fine.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda