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?
Hello, I have a small problem when using html2pdf and generating the pdf. I want to turn a
`let element = document.getElementById('container'); console.log('Elemento, ', element);
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...' );
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();