2

I need to put the content of a div into a pdf file then download it. I've searched a bit and I found that you can use javascript to do this via jsPDF.

I found this sample code but it seems that it doesn't work for me. When I click the generate pdf button nothing happens. I am also using codeigniter for my framework and my website is currently uploaded in 000.webhost.com.

 <div id="content"> <h3>Hello, this is a H3 tag</h3> <p>a pararaph</p> </div> <div id="editor"></div> <button id="cmd">generate PDF</button> <script type="text/javascript"> var doc = new jsPDF(); var specialElementHandlers = { '#editor': function (element, renderer) { return true; } }; $('#cmd').click(function () { doc.fromHTML($('#content').html(), 15, 15, { 'width': 170, 'elementHandlers': specialElementHandlers }); doc.save('sample-file.pdf'); }); </script> 
0

2 Answers 2

1

Works for me on JSFiddle.net: http://jsfiddle.net/scottcanoni/h3Lb05wa/

What error are you getting on your website?

All I am including is jQuery and the jsPDF script:

<script type="text/javascript" src="https://parall.ax/parallax/js/jspdf.js"></script> 

If you include both jQuery and jsPDF in your <head> tag, you should be fine with the code provided.

Sign up to request clarification or add additional context in comments.

7 Comments

there's no error. nothing happens when I click the generate pdf in my website. did you need to download the /parallax/js/jspdf.js ? I am a bit confused in where should I put jspdf js files in my document. I decided to put it together with my views. is that ok?
I updated my answer. You need to include the jsPDF in between the <head></head> tags on your page. Make sure it is there. Alternatively, you can download the JS and serve it from your own domain.
yeah. I have included it. should the js files be in the same folder as the views? (using codeigniter as framework)
I've managed to make it work but I have a new problem. I can't seem to work it with the contents of a div that has tables.
it's output in the pdf file is like this Copyright Pechay Productions 2014 ID Type Name Owner Contact # Address Accreditation Status 1 Apartment Nicoles Apartment It should be align horizontally.
|
0

Are you using JavaScript for a reason? I would (and have) use DOM PDF;

https://github.com/dompdf/dompdf

https://github.com/bcit-ci/CodeIgniter/wiki/PDF-generation-using-dompdf

1 Comment

could you create a simple code using the dom pdf? I can't understand it. I tried using the js because it's easy to understand and it is in the html file.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.