-2

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

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Faziki
  • 767
  • 1
  • 9
  • 24
  • have you tried this https://stackoverflow.com/questions/45420482/save-html-file-as-pdf/45421734 for example? – A. Meshu Nov 12 '19 at 22:54
  • 1
    Doing some basic research is kind of expected before asking a question on Stack Overflow. See the FAQ [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/q/261592/215552). – Heretic Monkey Nov 12 '19 at 23:00

1 Answers1

0

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.

  • @Shant Thanks you!, will have a look if it works and get back to you :) – Faziki Nov 12 '19 at 23:08
  • @A.Meshu html2canvas takes a screenshot image, need it to be pdf, word or excel depending on the content section thanks for the suggestion tho :) will surely use it in another project – Faziki Nov 12 '19 at 23:10