HTML to PDF on click convert

57 views Asked by At

I'm busy making a site and would like to know if there's a way to convert HTML to PDF or word file.

Scenario ~

User sees the information that he/she likes clicks on a button that converts the Page/Section to PDF or word and saves it locally (to the person's computer)

Any ideas? Anything would help if I could get some documentation or some direction it would be appreciated.

Thanks in advance.

Edit*

Sorry if it's a silly question still learning here :D

1

There are 1 answers

2
Shant Khayalian On

sure there are many ways, i can be helpful in JavaScript, i'm working on a project where client can download the Html files as PDF,please check

     <script>
            $(".clone-pdf").clone().appendTo(".html-content");

            function CreatePDFfromHTML() {
                var HTML_Width = $(".html-content").width();
                var HTML_Height = $(".html-content").height();
                var top_left_margin = 15;
                var PDF_Width = HTML_Width + (top_left_margin * 2);
                var PDF_Height = (PDF_Width * 1.5) + (top_left_margin * 2);
                var canvas_image_width = HTML_Width;
                var canvas_image_height = HTML_Height;

                var totalPDFPages = Math.ceil(HTML_Height / PDF_Height) - 1;

                html2canvas($(".html-content")[0]).then(function (canvas) {
                    var imgData = canvas.toDataURL("image/jpeg", 1.0);
                    var pdf = new jsPDF('p', 'pt', [PDF_Width, PDF_Height]);
                    pdf.addImage(imgData, 'JPG', top_left_margin, top_left_margin, canvas_image_width, canvas_image_height);
                    for (var i = 1; i <= totalPDFPages; i++) {
                        pdf.addPage(PDF_Width, PDF_Height);
                        pdf.addImage(imgData, 'JPG', top_left_margin, -(PDF_Height * i) + (top_left_margin * 4), canvas_image_width, canvas_image_height);
                    }
                    pdf.save("Your_PDF_Name.pdf");
                });
            }



  </script>

and the html to send the request to the script.

<a href="javascript:CreatePDFfromHTML()"><i class="icon-download"></i></a>

I wish i helps to get the answer you are looking for.