Export html to PDF using JavaScript

I want to export HTML to PDF using JavaScript, I have seen libraries like jsPDF and pdfMake , but they are very limited. For example, none of them can export HTML elements, such as <hr>with jsPDF the style is very limited, I saw this question, but the answer does not work for me, with pdfMake I can’t download pdf, just chrome .

+4
source share
1 answer

If you can get the HTML from the URL, you can check out this answer I wrote and explains how to do this.

Example:

https://dhtml2pdf.herokuapp.com/api.php?url=https://www.github.com&result_type=show

HTML, PDF HTML-.

PDF, https://www.github.com:

<a href="https://dhtml2pdf.herokuapp.com/api.php?url=https://www.github.com&result_type=show" target="_blank">Show PDF</a>

:

<a href="https://dhtml2pdf.herokuapp.com/api.php?url=https://www.github.com&result_type=download&file_name=my_pdf">Download PDF</a>

JavaScript, HTML PDF. :

HTML:

<button type="button" id="download-pdf-button">Download the PDF</button>

JavaScript:

document.getElementById("download-pdf-button").addEventListener("click", function() {
    var link = document.createElement('a');
    link.href = 'https://dhtml2pdf.herokuapp.com/api.php?url=https://www.github.com&result_type=download';
    link.download = 'file.pdf';
    link.dispatchEvent(new MouseEvent('click'));
});

Github.

, .

0

All Articles