Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert a web page into pdf by button click in html5

I have web page with containing data I want that when I click generate pdf button it should create pdf of that page. And Save Locally .

I have searched alot but I am getting script to only create pdf by entering data is there any other way

Below is the method which I tried but it is not suitable I want to create whole page into pdf file.

jspdf.com

I also tried another code but it also does not create the file

  <script>


  function generatePDF(){} 

  var conv = new ActiveXObject("pdfServMachine.converter");
  conv.convert("http://www.google.com", "c:\\google.pdf", false);
  WScript.Echo("finished conversion");
 }

 </script>
 <body onload="generatePDF()">
 </body>
 </html>
like image 713
user1808433 Avatar asked Nov 19 '12 06:11

user1808433


People also ask

How do I automatically convert HTML to PDF?

On a Windows computer, open an HTML web page in Internet Explorer, Google Chrome, or Firefox. On a Mac, open an HTML web page in Firefox. Click the “Convert to PDF” button in the Adobe PDF toolbar to start the PDF conversion. Enter a file name and save your new PDF file in a desired location.

How do I convert a URL to a PDF?

In the open web page, right-click the linked text and choose one of the following: To add the linked web page to an existing PDF, choose Append Link Target To Existing PDF. Then locate and select the existing PDF, and click Save. To convert the linked web page to a new PDF, choose Convert Link Target To Adobe PDF.


2 Answers

You didn't declare the function correctly. It should be generatePDF(){ instead of generatePDF(){} . Your } should be at the end of the function only.

like image 77
RRikesh Avatar answered Oct 31 '22 00:10

RRikesh


I am a bit late but let me tell you that, the code you are using is only compatible with old versions of Microsoft browser(i.e) because it contains WScript & ActiveX Component.

Yes, you can generate it with jsPDF.js. Just search this javascript file in google and download it locally then include it like shown below.

<script src="~/js/jspdf.js"></script>
<script>
  var doc = new jsPDF('p', 'pt', 'letter');
  var specialElementHandlers = {
    '#editor': function (element, renderer) {
      return true;
    }          
  };     
  $('#btn_Pdfprint').click(function () {
    doc.fromHTML($('#myTabContent').html(), 25, 25, {
      'width': 790,
      'elementHandlers': specialElementHandlers
    });
    doc.save('mywebpagee.pdf');
    window.location.reload();
  });
</script>
like image 44
user2929270 Avatar answered Oct 30 '22 22:10

user2929270