0

I have some logic in an Ionic 6 Angular app that outputs a pdf documents using jsPDF v2.5.1. The problem is it only produces a pdf the first time it is called and every time after that I am getting a blank pdf.

async printPdf(elementId: string) {
    const htmlBody = document.getElementById(elementId);

    let orientationIsPortrait = await this._currentOrientationIsPortrait();

    let doc = new jsPDF({
      orientation: "landscape"
    });

    await doc
      .html(htmlBody, {
        callback: this._completePdfShare,
        x: 0,
        y: 0,
        width: 280,
        windowWidth: orientationIsPortrait
          ? htmlBody.clientWidth * 4
          : htmlBody.clientWidth * 2,
      })
      .catch((error) => console.log(error));
  }

private async _completePdfShare(doc: jsPDF) {
    const fileName = `${new Date().getTime()}.pdf`;

    let pdfDocument = doc.output('datauristring', { filename: fileName });

    console.log(pdfDocument);

    //Share Doc
  }

The function is initiated by a button click from an ionic app page.

I did not find anything useful in my searches thus far.

Any help will be greatly appreciated.

Thanks

I have tried to instantiate the JsPDF document in the constructor but then the .html function produced the first document then kept on returning the first document throughout the life of the session.

I am expecting to get a pdf document representing the content of the active page on the app.

ikertz
  • 1
  • 1
  • I have never heard about this 'private' you are using. Please give a language reference to it — it might help. – Brice C. Jun 17 '23 at 09:46
  • I am using Typescript. – ikertz Jun 19 '23 at 20:23
  • What happens if you pass in "Hello World!" instead of `htmlBody`, does it generate consistently then? – Webber Jun 19 '23 at 20:27
  • Passing "Hello World!" seems to work but when I switch back to using HTMLElement the problem is still there. – ikertz Jun 19 '23 at 22:48
  • @ikertz — Ah, ah! Typescript. It might be useful to dig into that. As to private functions in vanilla JS, I use another trick (inside IIFEs). – Brice C. Jun 20 '23 at 09:18

0 Answers0