cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to generate a pdf file using javascript?

Former Member
0 Likes
1,566

I would like to generate a pdf file having images in it using javascript. I tried jsPDF but getting some exceptions like

Uncaught ReferenceError: pageWidth is not defined,

fail to load pdf document


Could anyone show me some pointers?


Thank You

Rahul

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Likes

Hi Rahul,

Did you find solution for pdf creation?

Thanks in advance,

Rashmi

Former Member
0 Likes

You can start trying out here, jsPDF - HTML5 PDF Generator | Parallax

Regards

Sakthivel

Former Member
0 Likes

i already tried it. but not getting the actual output. instead, encountering above mentioned errors.

Former Member
0 Likes

Hi Rahul,

I tried, it works fine. check the snippet http://jsbin.com/cicoh/1/edit

Regards

Sakthivel

Former Member
0 Likes

Could you please try for having an image in pdf ?

As of now, I'm using a different bootstrap than yours. I'll also try by mentioning ur bootstrap for jspdf.js

Former Member
0 Likes

Image in a pdf works, http://jsbin.com/cicoh/2/edit

Former Member
0 Likes

in that case, how can we convert normal image to data url programmatically?

Former Member
0 Likes

check the snippet, JS Bin - Collaborative JavaScript Debugging</title> <link rel="icon" href="h...

This coverts image to data-uri. But execute it in firefox or chrome with --disable-web-security flag as there is an issue with cross domain.

Regards

Sakthivel

Former Member
0 Likes

Hi Sakthivel,

This code is giving me error. Could you please help me out.

Thank You

Rahul

Former Member
0 Likes

check this, JS Bin - Collaborative JavaScript Debugging</title> <link rel="icon" href="h...  The previous one was throwing a security error due to CORS issue

Former Member
0 Likes

cool. will give a try now and let u know.

Thank you

Former Member
0 Likes

Here is my code

var myImg=new Image();

  myImg.src=arrData['employee'][1]['Image'];

  myImg.crossOrigin ="Anonymous";

   var imgData = this.getBase64Image(myImg);

   var doc = new jsPDF();

   doc.setFontSize(40);

   doc.text(35, 25, "Test Image");

   doc.addImage(myImg, 'JPEG', 15, 40, 180, 160);

   doc.save("sample1.pdf");

I'm receiving the following error at 'addImage' function.

Uncaught Error: getJpegSize could not find the size of the image


Could you please help in pointing out where am I going wrong.


Thank You

Former Member
0 Likes

If the image is already in the document (i.e. in the html page) then, your code is working perfect. But, it is giving me that error if I'm creating a image in the script.

Former Member
0 Likes

How about specifying the width & height for the Image.


myImg.style.width= '200px';

myImg.style.height= '200px';