javascript - Jspdf not working to export webpage -
i have downloaded jspdf , have run basic.html in examples folder. when select on of demos there no pdf exported. have added in basic.js script read export webpage there left out. using firefox 20.
here test page:test.html.
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>testt</title> <script type="text/javascript" src="js/jquery/jquery-1.7.1.min.js"></script> <script type="text/javascript" src="js/jquery/jquery-ui-1.8.17.custom.min.js"></script> <script type="text/javascript" src="jspdf.js"></script> <script type="text/javascript" src="libs/deflate/adler32cs.js"></script> <script type="text/javascript" src="libs/filesaver.js/filesaver.js"></script> <script type="text/javascript" src="libs/blob.js/blobbuilder.js"></script> <script type="text/javascript" src="jspdf.plugin.addimage.js"></script> <script type="text/javascript" src="jspdf.plugin.standard_fonts_metrics.js"></script> <script type="text/javascript" src="jspdf.plugin.split_text_to_size.js"></script> <script type="text/javascript" src="jspdf.plugin.from_html.js"></script> <script type="text/javascript" src="js/basic.js"></script> </head> <body> <script> </script> <a href="javascript:demotwopagedocument()" class="button">run code</a> </body> </html> this js script:basic.js
function htmldemo(){ var doc = new jspdf(); // we'll make our own renderer skip editor var specialelementhandlers = { '#editor': function(element, renderer){ return true; } }; // units in set measurement document // can changed "pt" (points), "mm" (default), "cm", "in" doc.fromhtml($('body').get(0), 15, 15, { 'width': 170, 'elementhandlers': specialelementhandlers }); } function demotwopagedocument() { var doc = new jspdf(); doc.text(20, 20, 'hello world!'); doc.text(20, 30, 'this client-side javascript, pumping out pdf.'); doc.addpage(); doc.text(20, 20, 'do that?'); // save pdf doc.save('test.pdf'); } function demolandscape() { var doc = new jspdf('landscape'); doc.text(20, 20, 'hello landscape world!'); // save pdf doc.save('test.pdf'); } function demofontsizes() { var doc = new jspdf(); doc.setfontsize(22); doc.text(20, 20, 'this title'); doc.setfontsize(16); doc.text(20, 30, 'this normal sized text underneath.'); doc.save('test.pdf'); } function demofonttypes() { var doc = new jspdf(); doc.text(20, 20, 'this default font.'); doc.setfont("courier"); doc.setfonttype("normal"); doc.text(20, 30, 'this courier normal.'); doc.setfont("times"); doc.setfonttype("italic"); doc.text(20, 40, 'this times italic.'); doc.setfont("helvetica"); doc.setfonttype("bold"); doc.text(20, 50, 'this helvetica bold.'); doc.setfont("courier"); doc.setfonttype("bolditalic"); doc.text(20, 60, 'this courier bolditalic.'); doc.save('test.pdf'); } function demotextcolors() { var doc = new jspdf(); doc.settextcolor(100); doc.text(20, 20, 'this gray.'); doc.settextcolor(150); doc.text(20, 30, 'this light gray.'); doc.settextcolor(255,0,0); doc.text(20, 40, 'this red.'); doc.settextcolor(0,255,0); doc.text(20, 50, 'this green.'); doc.settextcolor(0,0,255); doc.text(20, 60, 'this blue.'); // output data uri doc.output('datauri'); } function demometadata() { var doc = new jspdf(); doc.text(20, 20, 'this pdf has title, subject, author, keywords , creator.'); // optional - set properties on document doc.setproperties({ title: 'title', subject: 'this subject', author: 'james hall', keywords: 'generated, javascript, web 2.0, ajax', creator: 'meee' }); doc.save('test.pdf'); } function demouserinput() { var name = prompt('what name?'); var multiplier = prompt('enter number:'); multiplier = parseint(multiplier); var doc = new jspdf(); doc.setfontsize(22); doc.text(20, 20, 'questions'); doc.setfontsize(16); doc.text(20, 30, 'this belongs to: ' + name); for(var = 1; <= 12; ++) { doc.text(20, 30 + (i * 10), + ' x ' + multiplier + ' = ___'); } doc.addpage(); doc.setfontsize(22); doc.text(20, 20, 'answers'); doc.setfontsize(16); (i = 1; <= 12; ++) { doc.text(20, 30 + (i * 10), + ' x ' + multiplier + ' = ' + (i * multiplier)); } doc.save('test.pdf'); } function demorectangles() { var doc = new jspdf(); doc.rect(20, 20, 10, 10); // empty square doc.rect(40, 20, 10, 10, 'f'); // filled square doc.setdrawcolor(255, 0, 0); doc.rect(60, 20, 10, 10); // empty red square doc.setdrawcolor(255, 0, 0); doc.rect(80, 20, 10, 10, 'fd'); // filled square red borders doc.setdrawcolor(0); doc.setfillcolor(255, 0, 0); doc.rect(100, 20, 10, 10, 'f'); // filled red square doc.setdrawcolor(0); doc.setfillcolor(255, 0, 0); doc.rect(120, 20, 10, 10, 'fd'); // filled red square black borders doc.setdrawcolor(0); doc.setfillcolor(255, 255, 255); doc.roundedrect(140, 20, 10, 10, 3, 3, 'fd'); // black sqaure rounded corners doc.save('test.pdf'); } function demolines() { var doc = new jspdf(); doc.line(20, 20, 60, 20); // horizontal line doc.setlinewidth(0.5); doc.line(20, 25, 60, 25); doc.setlinewidth(1); doc.line(20, 30, 60, 30); doc.setlinewidth(1.5); doc.line(20, 35, 60, 35); doc.setdrawcolor(255,0,0); // draw red lines doc.setlinewidth(0.1); doc.line(100, 20, 100, 60); // vertical line doc.setlinewidth(0.5); doc.line(105, 20, 105, 60); doc.setlinewidth(1); doc.line(110, 20, 110, 60); doc.setlinewidth(1.5); doc.line(115, 20, 115, 60); // output data uri doc.output('datauri'); } function democircles() { var doc = new jspdf(); doc.ellipse(40, 20, 10, 5); doc.setfillcolor(0,0,255); doc.ellipse(80, 20, 10, 5, 'f'); doc.setlinewidth(1); doc.setdrawcolor(0); doc.setfillcolor(255,0,0); doc.circle(120, 20, 5, 'fd'); doc.save('test.pdf'); } function demotriangles() { var doc = new jspdf(); doc.triangle(60, 100, 60, 120, 80, 110, 'fd'); doc.setlinewidth(1); doc.setdrawcolor(255,0,0); doc.setfillcolor(0,0,255); doc.triangle(100, 100, 110, 100, 120, 130, 'fd'); doc.save('test.pdf'); } function demoimages() { // because of security restrictions, getimagefromurl // not load images other domains. chrome has added // security restrictions prevent loading images // when running local files. run with: chromium --allow-file-access-from-files --allow-file-access // temporarily around issue. var getimagefromurl = function(url, callback) { var img = new image(), data, ret = { data: null, pending: true }; img.onerror = function() { throw new error('cannot load image: "'+url+'"'); }; img.onload = function() { var canvas = document.createelement('canvas'); document.body.appendchild(canvas); canvas.width = img.width; canvas.height = img.height; var ctx = canvas.getcontext('2d'); ctx.drawimage(img, 0, 0); // grab image jpeg encoded in base64, data data = canvas.todataurl('image/jpeg').slice('data:image/jpeg;base64,'.length); // convert data binary form data = atob(data); document.body.removechild(canvas); ret['data'] = data; ret['pending'] = false; if (typeof callback === 'function') { callback(data); } }; img.src = url; return ret; }; // since images loaded asyncronously, must wait create // pdf until have image data. // if had jpeg image binary data loaded // string, create pdf without delay. var createpdf = function(imgdata) { var doc = new jspdf(); doc.addimage(imgdata, 'jpeg', 10, 10, 50, 50); doc.addimage(imgdata, 'jpeg', 70, 10, 100, 120); doc.save('output.pdf'); } getimagefromurl('thinking-monkey.jpg', createpdf); } function demostringsplitting() { var pdf = new jspdf('p','in','letter') , sizes = [12, 16, 20] , fonts = [['times','roman'],['helvetica',''], ['times','italic']] , font, size, lines , margin = 0.5 // inches on 8.5 x 11 inch sheet. , verticaloffset = margin , loremipsum = 'lorem ipsum dolor sit amet, consectetur adipiscing elit. phasellus id eros turpis. vivamus tempor urna vitae sapien mollis molestie. vestibulum in lectus non enim bibendum laoreet @ at libero. etiam malesuada erat sed sem blandit in varius orci porttitor. sed @ sapien urna. fusce augue ipsum, molestie et adipiscing at, varius quis enim. morbi sed magna est, vel vestibulum urna. sed tempor ipsum vel mi pretium @ elementum urna tempor. nulla faucibus consectetur felis, elementum venenatis mi mollis gravida. aliquam mi ante, accumsan eu tempus vitae, viverra quis justo.\n\nproin feugiat augue in augue rhoncus eu cursus tellus laoreet. pellentesque eu sapien @ diam porttitor venenatis nec vitae velit. donec ultrices volutpat lectus eget vehicula. nam eu erat mi, in pulvinar eros. mauris viverra porta orci, et vehicula lectus sagittis id. nullam @ magna vitae nunc fringilla posuere. duis volutpat malesuada ornare. nulla in eros metus. vivamus posuere libero.' // margins: pdf.setdrawcolor(0, 255, 0) .setlinewidth(1/72) .line(margin, margin, margin, 11 - margin) .line(8.5 - margin, margin, 8.5-margin, 11-margin) // 3 blocks of text (var in fonts){ if (fonts.hasownproperty(i)) { font = fonts[i] size = sizes[i] lines = pdf.setfont(font[0], font[1]) .setfontsize(size) .splittexttosize(loremipsum, 7.5) // don't want preset font, size calculate lines? // .splittexttosize(text, maxsize, options) // allows pass object of following: // { // 'fontsize': 12 // , 'fontstyle': 'italic' // , 'fontname': 'times' // } // without these, .splittexttosize use current / default // font family, style, size. console.log(lines); pdf.text(0.5, verticaloffset + size / 72, lines) verticaloffset += (lines.length + 0.5) * size / 72 } } pdf.save('test.pdf'); } function demofromhtml() { var pdf = new jspdf('p', 'in', 'letter'); // source can html-formatted string, or reference // actual dom element text scraped. var source = $('#fromhtmltestdiv')[0] // support special element handlers. register them jquery-style // id selector either id or node name. ("#iamid", "div", "span" etc.) // there no support other type of selectors // (class, of compound) @ time. , specialelementhandlers = { // element id of "bypass" - jquery style selector '#bypassme': function(element, renderer){ // true = "handled elsewhere, bypass text extraction" return true } } // coords , widths in jspdf instance's declared units // 'inches' in case pdf.fromhtml( source // html string or dom elem ref. , 0.5 // x coord , 0.5 // y coord , { 'width':7.5 // max width of content on pdf , 'elementhandlers': specialelementhandlers } ) pdf.save('test.pdf'); }
10/31/13 - had same problem. fixed removing semicolon @ end of line 312 of basic.js file.
Comments
Post a Comment