Generate PDF with JavaScript

JSPdf provides an API to generate PDF files on-the-fly using JavaScript.
This API is supported by all the HTML5 browsers ( IE6+*, Firefox 3+, Chrome, Safari 3+, Opera)
Download Latest JSPDF library from https://github.com/MrRio/jsPDF/zipball/master

1) How to Generate PDF with Javascript

//So simple
var doc = new jsPDF();
doc.setFontSize(22);
doc.text(20, 20, 'My First PDF');
doc.addPage();
doc.setFontSize(16);
doc.text(20, 30, 'This is some normal sized text underneath.');

 

List of APIs provided by JSPDF.

//Create PDf
var doc = new jsPDF();

//Create Landscape PDF
var doc = new jsPDF('landscape');

//Saving docment using data-uri
doc.save('out.pdf')

//get the PDF buffer
doc.output() 

//Set PDF properities
doc.setProperties({
	title: 'PDF Title',
	subject: 'This is the subject',		
	author: 'Ravishanker Kusuma',
	keywords: 'pdf, javascript,geenerated',
	creator: 'KRS'
});

//Set Font type,size & details
doc.setFont("times");
doc.setFontType("italic");
doc.setFontSize(16);

//Add text to pdf
doc.text(X,Y, 'This is courier bolditalic.'); //X,Y are the position

//Change Text color
doc.setTextColor(150);

//Adding a page
doc.addPage();

//Adding a Line
doc.setLineWidth(0.5);
doc.line(X1, Y1, X2, Y2); // horizontal line

//Draw Rectangle
doc.setDrawColor(0);
doc.setFillColor(255,0,0);
doc.rect(X1, Y1, WIDTH, HEIGHT, 'F');  //F is for Fill
doc.rect(X1, Y1, WIDTH, HEIGHT, 'D');  //D is for Draw
doc.rect(X1, Y1, WIDTH, HEIGTH, 'FD');  //FD is for Fill and Draw 

//Draw Triangle
doc.setLineWidth(1);
doc.setDrawColor(255,0,0);
doc.setFillColor(0,0,255);
doc.triangle(X1, Y1, X2, Y2, X3, Y3, 'FD');//FD is for Fill and Draw

2).Download the generated PDF File

JSPDF lib generates pdf at the browser and provides an option(using data-uri) to download. But it is not working all browsers.
You can download the generated PDF using Downloadify plugin.
Downloadify plugin provides an option to download any file(not only pdf) generated at browser.

	
//'downloadify' is the div id
//'Simple.pdf' is the output file.
Downloadify.create('downloadify',{
			filename: 'Simple.pdf',
			data: function()
			{ 
					var doc = new jsPDF();
					doc.setFontSize(22);
					doc.text(20, 20, 'FirstPage: My First PDF');
					doc.addPage();
					doc.setFontSize(16);
					doc.text(20, 30, 'Second Page This is some normal sized text underneath.');	
				return doc.output();
			},
			onComplete: function(){ alert('Your File Has Been Saved!'); },
			onCancel: function(){ alert('You have cancelled the saving of this file.'); },
			onError: function(){ alert('You must put something in the File Contents or there will be nothing to save!'); },
			downloadImage: 'images/download.png',
			swf: 'images/downloadify.swf',
			width: 100,
			height: 30,
			transparent: true,
			append: false
		});