Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
Canvas no respeta los márgenes en un archivo PDF Javascript

Esta es mi primera pregunta aquí, espero que me puedan ayudar. Estoy tratando de crear un archivo PDF con 160 códigos qr y algo de texto, escribí una función para separar cada qr y sus textos, pero creo que me falta algo al dibujarlos todos en el lienzo/pdf final. Porque todos ellos están posicionados en el margen superior e izquierdo. Y tampoco respeta el margen del texto.

Este es el código que estoy ejecutando:

 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') })

Este es el resultado final.

imagen pdf con codigos

¡Gracias a todos!

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda