convert pdf files with pdfjs

Asked

Viewed 24 times

0

I have a component to send files to the back end, but I need these files to be transformed into pdf. Thinking about it, I am using Pdfjs, passing the output element to it, but I noticed that it is not working.

follows my Cod

testePdf(file) {
    let doc = new jsPDF();
    let content = this.upload.nativeElement;
    doc.fromHTML(content.innerHTML, 15, 15, {
      'width': 190,
    });
    doc.save('test.pdf');
  }

he’s returning empty, where I’m erring?

I am using uppy Dashboard for upload

1 answer

0

Maybe I can help with some idea. I had a similar problem using jsPDF, it seems that the html methods are deprecated (both the addHTML as to the fromHTML), the exit I found was the following:

install html2canvas and the result of canvas, play as image to PDF:

generatePdf(element: HTMLElement, filename: string): Promise<void> {
  const options = {
    background: 'white',
    imageTimeout: 1000,
    useCORS: true,
    scale: 1.5
  };

  html2canvas(element, options).then((canvas) => {
    const img = canvas.toDataURL("image/PNG");
    const doc = new jsPDF('p', 'pt', 'a4');

    // Add image Canvas to PDF
    const bufferX = 5;
    const bufferY = 5;
    const imgProps = (<any>doc).getImageProperties(img);
    const pdfWidth = doc.internal.pageSize.getWidth() - 2 * bufferX;
    const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width;
    doc.addImage(img, 'PNG', bufferX, bufferY, pdfWidth, pdfHeight, undefined, 'FAST');

    return doc;
  }).then((doc) => {
    return doc.save(filename);
  });
}
  • I thought about it, but the user can go up to an xlr file, worried that I do not know if it meets

Browser other questions tagged

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