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

136
Visualizações
Canvas is not respecting margins in a PDF file Javascript

This is my first question here, I hope you can help me. I'm trying to create a PDF file with 160 qr codes and some text, I wrote one function to separete each qr and its texts, but I think I'm missing something when drawing all of them in the final canva/pdf. Because all of them are positioned at the top and left margin. And it doesn't respect the margin of the text neither.

This is the code I'm running:

const QrQuantity = 160
const pageSize = { width: 1680.12, height: 1134.77 }
const fontSize = 7
const qrSize = 45
const marginCanvasX = 55.96
const marginCanvasY = 28.89
const marginQrX = 55.16
const marginQrY = 66.25
const rows = 10
const columns = 16
const textMargin = 4

function drawQR(ctx, img, id, column, row) {
  ctx.save()
  const offsetX = (column * qrSize) + ((marginQrX + fontSize) * column)
  const offsetY = (row * qrSize) + ((marginQrY + fontSize) * row)
  // Set the position to where we need to draw the QR
  ctx.translate(offsetX, offsetY)
  // Draw the QR code
  ctx.drawImage(img, 0, 0, qrSize, qrSize)
  // Write "My web"
  ctx.fillText('myweb.com', 8, qrSize + fontSize + textMargin)
  ctx.save()
  // Rotate to write vertical text
  ctx.rotate(Math.PI / 2)
  // Write the ID
  ctx.fillText(id, 0, -qrSize - 3)
  // Undo all the translations and rotations
  ctx.restore()
  ctx.restore()
}

function downloadPDF(qrs) {
  // Create the context
  const ctx = new canvas2pdf.PdfContext(blobStream(), { size: [pageSize.width, pageSize.height] })
  // Set the base styles
  ctx.fillStyle = '#000'
  ctx.font = `${fontSize}px`
  // Apply left and top margins
  ctx.translate(marginCanvasX, marginCanvasY)
  // When we're done drawing, download the image as a PDF document
  ctx.stream.on('finish', () => {
    const blob = ctx.stream.toBlob('application/pdf')
    download(blob, 'codes.pdf')
  })

This is the final result.

pdf image with codes

Thank You all!

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