-1

I'm trying to Export a URL to PDF. As shown in the code below, solutionUrl is a link to an article. I want to be able to see the article in the saved pdf file. Right now it only shows the link inside the pdf file.

 component.st: 

 downloadPDF() {
    const doc = new jsPDF();
    doc.text(this.solutionUrl, 10, 10 );
    doc.save('Solution.pdf');
  }
component.html: 

  <button title="Export to PDF" (click)="downloadPDF()"> <i class="glyphicon glyphicon-file"></i></button>
Anjela
  • 1
  • 1

1 Answers1

0

I haven't worked with jsPDF but this could be because doc.text expects a string as a parameter, not a URL.

Also to display the contents of a HTML page in PDF, you need to use the doc.addHTML method

So your code would flow like

  1. Get the content of the URL using this.http.get(url).subscribe(res => parseAndSaveToPDF(res)));
  2. Then define the parseAndSaveToPDF function to parse the content of the web page and extract the body element
  3. Finally use doc.addHTML(yourParsedBody) to get the body value as a PDF.

Here is another SO answer that might be useful

S Raghav
  • 1,386
  • 1
  • 16
  • 26
  • I tried your example , it is not working. doc.addHTML doesn't work. Do you know another way to export a url content to pdf? – Anjela Mar 15 '18 at 19:29
  • @Anjela what happens when you try `doc.addHTML` ? Is an error thrown or do you get the HTML tags and not the content only? – S Raghav Mar 16 '18 at 07:19
  • I can't use addHTML the way you explained it, i need to use parameters in it and a function. this is the error message: Error: You need either https://github.com/niklasvh/html2canvas or https://github.com/cburgmer/rasterizeHTML.js – Anjela Mar 16 '18 at 15:33
  • @Anjela yes. that is expected. If you see the [SO answer](https://stackoverflow.com/questions/28193487/how-to-use-addhtml-function-in-jspdf) I linked to in the answer, it requires you to include one of these two dependencies – S Raghav Mar 16 '18 at 17:38