eKoopmans/html2pdf.js

Generate PDF returns undefined, not pdf file as expected.

inigopanos opened this issue · 1 comments

Hello, I have a small problem when using html2pdf and generating the pdf. I want to turn a

called container into a pdf. I am the following options to generate it:

`let element = document.getElementById('container');
console.log('Elemento, ', element);

  let opt = {
    margin: 1,
    filename: `formulario_asistencia_detenido_${timestamp}`,

    image: { type: 'jpeg', quality: 0.98 },
    html2canvas: { scale: 2 },
    jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' },
  };`

To then use an async function to generate the PDF:

async function generatePDF(element: any, opt: any): Promise<any> { try { let pdf = await html2pdf(opt).from(element).save().outputPdf(); console.log('PDF generado exitosamente:', pdf); return pdf; } catch (error) { console.error('Error al generar el PDF:', error, element, opt); } }

Finally, I am calling that function to create the pdf and, if it is a pdf, send it to my API:

`generatePDF(element, opt).then((pdf) => {
if (pdf?.type !== 'application/pdf') {
console.log(
'Tipo de archivo no válido. Debe ser un archivo PDF. Probablemente sea undefined...'
);

    } else {
      console.log('Tipo de archivo válido, es de tipo PDF');        
      FormularioServices.enviarFormulario(pdf, opt.filename);
    }
  });`

The problem comes when trying to generate the pdf, it gives an error calling it an Unkown source type, and further logging says it is type undefined.

My question is, is there anything in this process I am missing, or doing wrong? Any tips on how can I successfully generate the pdf?

Thanks.

dont use anything after save :- const exporter = new html2pdf();
const test = exporter.set(options).from(container).toCanvas().save();