Add Jspdf image

Asked

Viewed 1,720 times

0

I have this script that generates a pdf of a div. Bad images that contain in div are not generated in the pdf, someone can help me?

$('#downloadpdf').click(function() 
                {
                    var doc = new jsPDF('landscape', 'pt', 'a4');
                        doc.addHTML($('#dowextrato'), function()
                    {
                        doc.save("relatorio-<?php echo $Pnome; ?>.pdf");
                    }); 
                });

2 answers

0

Para React:

Create a HTMLImageElement and use it as a function parameter doc.addImage

import autoTable  from 'jspdf-autotable'
import jsPDF from 'jspdf'

const image = document.createElement('img')
image.setAttribute('src', '/assets/images/imagem.png')

const gerarPDF = () => {
  const doc = new jsPDF()
  autoTable(doc, {
    margin: {top: 40},  //"empurra" a tabela para baixo, deixando espaco para imagem
    head: cabecalho,
    body: dados,
    didDrawPage: data => {
      doc.addImage(image, 'PNG', data.settings.margin.left, 15, 60, 20)
    }
  })
  doc.save('nomeDoArquivo')
}

0


  • That was perfect! Now, without wanting to abuse: "15,40,180,160" which are the positions?

  • The parameters are: doc.addImage(image, 'image format', x, y, image width, image height);

  • Thanks a friend, thanks a lot!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.