Get Your Pdf button for Your Web Site

Get your pdf button and convert your html content in pdf document in a instant with jsPDF

// get jsPDF library - url
var jsPDF = "http://mrrio.github.io/jsPDF/dist/jspdf.debug.js"
// set css class for button
var pdfclass = ".pdf{-moz-box-shadow:inset 0 1px 0 0 #f5978e;-webkit-box-shadow:inset 0 1px 0 0 #f5978e;box-shadow:inset 0 1px 0 0 #f5978e;background-color:#f24537;-webkit-border-top-left-radius:16px;-moz-border-radius-topleft:16px;border-top-left-radius:16px;-webkit-border-top-right-radius:16px;-moz-border-radius-topright:16px;border-top-right-radius:16px;-webkit-border-bottom-right-radius:16px;-moz-border-radius-bottomright:16px;border-bottom-right-radius:16px;-webkit-border-bottom-left-radius:16px;-moz-border-radius-bottomleft:16px;border-bottom-left-radius:16px;text-indent:0;border:1px solid #d02718;display:inline-block;color:#fff;font-family:Arial;font-size:15px;font-weight:bold;font-style:normal;height:27px;line-height:27px;width:120px;text-decoration:none;text-align:center;text-shadow:1px 1px 0 #810e05}.pdf:hover{background-color:#c62d1f}.pdf:active{position:relative;top:1px}";
var head = document.head || document.getElementsByTagName('head')[0];
// create style button
var style = document.createElement('style');
var btnpdf = document.createElement("button");
style.type = 'text/css';
if (style.styleSheet){
  style.styleSheet.cssText = pdfclass;
} else {
  style.appendChild(document.createTextNode(pdfclass));
}
head.appendChild(style);
// append pdf library
var scriptjspdf =  document.createElement('script');
scriptjspdf.src = jsPDF;
head.appendChild(scriptjspdf);
btnpdf.setAttribute("id", "printpdf");
btnpdf.setAttribute("class", "pdf");
var textBtn = document.createTextNode("Save PDF");
btnpdf.appendChild(textBtn)
document.body.appendChild(btnpdf);
// now build your pdf function
function saveinPdf(){	
	var pdf = new jsPDF('p', 'pt', 'letter')
	, source = document.body
	, specialElementHandlers = {
		'#bypassme': function(element, renderer){
			return true
		}
	}

   margins = {
      top: 20,
      bottom: 20,
      left: 20,
      width: 210
    };
    pdf.fromHTML(
    	source 
    	, margins.left 
    	, margins.top 
    	, {
    		'width': margins.width 
    		, 'elementHandlers': specialElementHandlers
    	},
    	function (dispose) {
	    	try {
	    		var inputFilename = prompt("Enter file name to save html result in pdf");
		    		if(inputFilename == "") {
		    			alert('You must enter file name to save result in pdf, please')
		    		} else {
		    			pdf.save(inputFilename + '.pdf');
		    		}	    		
	    		} catch(e) {
	    			 txt="There was an error on save.\n\n";
	    			 txt+="Error description: " + e.message + "\n\n";
	    			 txt+="Click OK to continue.\n\n";
	    			 alert(txt);
	    		}
    		
        },
    	margins
    )	
}
document.getElementById("printpdf").addEventListener("click", saveinPdf, false);



Nessun commento


You can leave the first : )