basic.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <!doctype>
  2. <html>
  3. <head>
  4. <title>jsPDF</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  6. <link rel="stylesheet" type="text/css" href="css/smoothness/jquery-ui-1.8.17.custom.css">
  7. <link rel="stylesheet" type="text/css" href="css/main.css">
  8. <script type="text/javascript" src="js/jquery/jquery-1.7.1.min.js"></script>
  9. <script type="text/javascript" src="js/jquery/jquery-ui-1.8.17.custom.min.js"></script>
  10. <script type="text/javascript" src="../dist/jspdf.debug.js"></script>
  11. <script type="text/javascript" src="js/basic.js"></script>
  12. <script>
  13. $(function() {
  14. $("#accordion-basic, #accordion-text, #accordion-graphic").accordion({
  15. autoHeight: false,
  16. navigation: true
  17. });
  18. $( "#tabs" ).tabs();
  19. $(".button").button();
  20. });
  21. </script>
  22. </head>
  23. <body>
  24. <a href="https://github.com/MrRio/jsPDF">
  25. <img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub" />
  26. </a>
  27. <h1>jsPDF Demos</h1>
  28. <p>Examples for using jsPDF with Data URIs below. Go <a href="https://github.com/MrRio/jsPDF">back to project homepage</a>.</p>
  29. <div id="tabs">
  30. <ul>
  31. <li><a href="#tabs-basic">Basic elements</a></li>
  32. <li><a href="#tabs-text">Text elements</a></li>
  33. <li><a href="#tabs-graphic">Graphic elements</a></li>
  34. </ul>
  35. <div id="tabs-basic">
  36. <div id="accordion-basic">
  37. <h2><a href="#">Simple two-page text document</a></h2>
  38. <div><p><pre>var doc = new jsPDF();
  39. doc.text(20, 20, 'Hello world!');
  40. doc.text(20, 30, 'This is client-side Javascript, pumping out a PDF.');
  41. doc.addPage();
  42. doc.text(20, 20, 'Do you like that?');
  43. doc.save('Test.pdf');</pre>
  44. <a href="javascript:demoTwoPageDocument()" class="button">Run Code</a></p></div>
  45. <h2><a href="#">Landscape document</a></h2>
  46. <div><p><pre>var doc = new jsPDF('landscape');
  47. doc.text(20, 20, 'Hello landscape world!');
  48. doc.save('Test.pdf');</pre>
  49. <a href="javascript:demoLandscape()" class="button">Run Code</a></p></div>
  50. <h2><a href="#">Adding metadata</a></h2>
  51. <div><p><pre>var doc = new jsPDF();
  52. doc.text(20, 20, 'This PDF has a title, subject, author, keywords and a creator.');
  53. // Optional - set properties on the document
  54. doc.setProperties({
  55. title: 'Title',
  56. subject: 'This is the subject',
  57. author: 'James Hall',
  58. keywords: 'generated, javascript, web 2.0, ajax',
  59. creator: 'MEEE'
  60. });
  61. // Output as Data URI
  62. doc.save('Test.pdf');</pre>
  63. <a href="javascript:demoMetadata()" class="button">Run Code</a></p></div>
  64. <h2><a href="#">Example of user input</a></h2>
  65. <div><p><pre>var name = prompt('What is your name?');
  66. var multiplier = prompt('Enter a number:');
  67. multiplier = parseInt(multiplier);
  68. var doc = new jsPDF();
  69. doc.setFontSize(22);
  70. doc.text(20, 20, 'Questions');
  71. doc.setFontSize(16);
  72. doc.text(20, 30, 'This belongs to: ' + name);
  73. for(var i = 1; i <= 12; i ++) {
  74. doc.text(20, 30 + (i * 10), i + ' x ' + multiplier + ' = ___');
  75. }
  76. doc.addPage();
  77. doc.setFontSize(22);
  78. doc.text(20, 20, 'Answers');
  79. doc.setFontSize(16);
  80. for(var i = 1; i <= 12; i ++) {
  81. doc.text(20, 30 + (i * 10), i + ' x ' + multiplier + ' = ' + (i * multiplier));
  82. }
  83. doc.save('Test.pdf');</pre>
  84. <a href="javascript:demoUserInput()" class="button">Run Code</a></p></div>
  85. </div>
  86. </div>
  87. <div id="tabs-text">
  88. <div id="accordion-text">
  89. <h2><a href="#">Different font sizes</a></h2>
  90. <div><p><pre>var doc = new jsPDF();
  91. doc.setFontSize(22);
  92. doc.text(20, 20, 'This is a title');
  93. doc.setFontSize(16);
  94. doc.text(20, 30, 'This is some normal sized text underneath.');
  95. doc.save('Test.pdf');</pre>
  96. <a href="javascript:demoFontSizes()" class="button">Run Code</a>
  97. </p></div>
  98. <h2><a href="#">Different font types</a></h2>
  99. <div><p><pre>var doc = new jsPDF();
  100. doc.text(20, 20, 'This is the default font.');
  101. doc.setFont("courier");
  102. doc.text(20, 30, 'This is courier normal.');
  103. doc.setFont("times");
  104. doc.setFontType("italic");
  105. doc.text(20, 40, 'This is times italic.');
  106. doc.setFont("helvetica");
  107. doc.setFontType("bold");
  108. doc.text(20, 50, 'This is helvetica bold.');
  109. doc.setFont("courier");
  110. doc.setFontType("bolditalic");
  111. doc.text(20, 60, 'This is courier bolditalic.');
  112. doc.save('Test.pdf');</pre>
  113. <a href="javascript:demoFontTypes()" class="button">Run Code</a></p></div>
  114. <h2><a href="#">Different text colors</a></h2>
  115. <div><p><pre>var doc = new jsPDF();
  116. doc.setTextColor(100);
  117. doc.text(20, 20, 'This is gray.');
  118. doc.setTextColor(150);
  119. doc.text(20, 30, 'This is light gray.');
  120. doc.setTextColor(255,0,0);
  121. doc.text(20, 40, 'This is red.');
  122. doc.setTextColor(0,255,0);
  123. doc.text(20, 50, 'This is green.');
  124. doc.setTextColor(0,0,255);
  125. doc.text(20, 60, 'This is blue.');
  126. doc.save('Test.pdf');</pre>
  127. <a href="javascript:demoTextColors()" class="button">Run Code</a></p></div>
  128. <h2><a href="#">Font-metrics-based line sizing and split</a></h2>
  129. <div><p><pre>var pdf = new jsPDF('p','in','letter')
  130. , sizes = [12, 16, 20]
  131. , fonts = [['Times','Roman'],['Helvetica',''], ['Times','Italic']]
  132. , font, size, lines
  133. , verticalOffset = 0.5 // inches on a 8.5 x 11 inch sheet.
  134. , loremipsum = 'Lorem ipsum dolor sit amet, ...'
  135. for (var i in fonts){
  136. if (fonts.hasOwnProperty(i)) {
  137. font = fonts[i]
  138. size = sizes[i]
  139. lines = pdf.setFont(font[0], font[1])
  140. .setFontSize(size)
  141. .splitTextToSize(loremipsum, 7.5)
  142. // Don't want to preset font, size to calculate the lines?
  143. // .splitTextToSize(text, maxsize, options)
  144. // allows you to pass an object with any of the following:
  145. // {
  146. // 'fontSize': 12
  147. // , 'fontStyle': 'Italic'
  148. // , 'fontName': 'Times'
  149. // }
  150. // Without these, .splitTextToSize will use current / default
  151. // font Family, Style, Size.
  152. pdf.text(0.5, verticalOffset + size / 72, lines)
  153. verticalOffset += (lines.length + 0.5) * size / 72
  154. }
  155. }
  156. pdf.save('Test.pdf');</pre>
  157. <a href="javascript:demoStringSplitting()" class="button">Run Code</a></p></div>
  158. <h2><a href="#">fromHTML plugin</a></h2>
  159. <div class="to_pdf">
  160. <div><p>This (BETA level. API is subject to change!) plugin allows one to scrape formatted text from an HTML fragment into PDF. Font size, styles are copied. The long-running text is split to stated content width.</p></div>
  161. <div style="border-width: 2px; border-style: dotted; padding: 1em; font-size:120%;line-height: 1.5em;" id="fromHTMLtestdiv">
  162. <h2 style="font-size:120%">Header Two</h2>
  163. <strong><em>Double style span</em></strong>
  164. <span style="font-family:monospace">Monotype span with
  165. carriage return. </span><span style="font-size:300%">a humongous font size span.</span>
  166. Followed by long parent-less text node. asdf qwer asdf zxcv qsasfd qwer qwasfd zcxv sdf qwer qwe sdf wer qwer asdf zxv.
  167. <div <span style="font-family:serif">Serif Inner DIV (bad markup, but testing block detection)</div><span style="font-family:sans-serif"> Sans-serif span with extra spaces </span>
  168. Followed by text node without any wrapping element. <span>And some long long text span attached at the end to test line wrap. qwer asdf qwer lkjh asdf zxvc safd qwer wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww qewr asdf zxcv.</span>
  169. <p style="font-size:120%">This is a <em style="font-size:120%">new</em> paragraph.</p>
  170. This is more wrapping-less text.
  171. <p id="bypassme" style="font-size:120%">This paragraph will <strong style="font-size:120%">NOT</strong> be on resulting PDF because a special attached element handler will be looking for the ID - 'bypassme' - and should bypass rendering it.</p>
  172. <p style="font-size:120%;text-align:center">This is <strong style="font-size:120%">another</strong> paragraph.</p>
  173. <p style="text-align:justify">
  174. Integer dignissim urna tortor? Cum rhoncus, a lacus ultricies tincidunt, tristique lundium enim urna, magna? Sed, enim penatibus? Lacus pellentesque integer et pulvinar tortor? Dapibus in arcu arcu, vut dolor? Et! Placerat pulvinar cursus, urna ultrices arcu nunc, a ultrices dictumst elementum? Magnis rhoncus pellentesque, egestas enim purus, augue et nascetur sociis enim rhoncus. Adipiscing augue placerat tincidunt pulvinar ridiculus. Porta in sociis arcu et placerat augue sit enim nec hac massa, turpis ridiculus nunc phasellus pulvinar proin sit pulvinar, ultrices aliquet placerat amet? Lorem nunc porttitor etiam risus tempor placerat amet non hac, nunc sed odio augue? Turpis, magnis. Lorem pid, a porttitor tincidunt adipiscing sagittis pellentesque, mattis amet, duis proin, penatibus lectus lorem eros, nisi, tempor phasellus, elit.
  175. </p>
  176. <h2>Image Support</h2>
  177. <p>
  178. NOTES: the img src must be on the same domain or the external domain should allow Cross-origin.
  179. </p>
  180. <img src="http://maps.googleapis.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=13&size=400x300&scale=1&maptype=roadmap&markers=color:blue%7Clabel:S%7C40.702147,-74.015794&markers=color:green%7Clabel:G%7C40.711614,-74.012318&markers=color:red%7Ccolor:red%7Clabel:C%7C40.718217,-73.998284&sensor=false" width="400" height="300">
  181. <!-- ADD_PAGE -->
  182. <h2>New page added with html comment: ADD_PAGE</h2>
  183. <h2></h2>
  184. <p>HTML Table:</p>
  185. <p>
  186. NOTES: Must set the COLGROUP tag with "with" on each COL tag as %, inspect the table. BTW the css does not have a good style to render the table on the html :P, feel free to the add the CSS.
  187. </p>
  188. <table>
  189. <colgroup>
  190. <col width="60%">
  191. <col width="40%">
  192. </colgroup>
  193. <thead>
  194. <tr>
  195. <th>
  196. Heading1
  197. </th>
  198. <th>
  199. Heading2
  200. </th>
  201. </tr>
  202. </thead>
  203. <tbody>
  204. <tr>
  205. <td>
  206. cell 1,1
  207. </td>
  208. <td>
  209. cell 1,2
  210. </td>
  211. </tr>
  212. <tr>
  213. <td>
  214. cell 2,1
  215. </td>
  216. <td>
  217. cell 2,2
  218. </td>
  219. </tr>
  220. <tr>
  221. <td>
  222. cell 3,1
  223. </td>
  224. <td>
  225. cell 3,2
  226. </td>
  227. </tr>
  228. <tr>
  229. <td>
  230. cell 4,1
  231. </td>
  232. <td>
  233. cell 4,2
  234. </td>
  235. </tr>
  236. </tvody>
  237. </table>
  238. <h2></h2>
  239. <h2></h2>
  240. <p>HTML Lists:</p>
  241. <div style="margin-left:20px">
  242. <ul>
  243. <li>Lorem Ipsum</li>
  244. <li>Dolor Sit amen</li>
  245. <li>Lorem Ipsum</li>
  246. <li>Dolor Sit amen</li>
  247. </ul>
  248. <ol>
  249. <li>Lorem Ipsum</li>
  250. <li>Dolor Sit amen</li>
  251. <li>Lorem Ipsum</li>
  252. <li>Dolor Sit amen</li>
  253. </ol>
  254. </div>
  255. </div>
  256. <div><p><pre>var pdf = new jsPDF('p', 'pt', 'letter')
  257. // source can be HTML-formatted string, or a reference
  258. // to an actual DOM element from which the text will be scraped.
  259. , source = $('#fromHTMLtestdiv')[0]
  260. // we support special element handlers. Register them with jQuery-style
  261. // ID selector for either ID or node name. ("#iAmID", "div", "span" etc.)
  262. // There is no support for any other type of selectors
  263. // (class, of compound) at this time.
  264. , specialElementHandlers = {
  265. // element with id of "bypass" - jQuery style selector
  266. '#bypassme': function(element, renderer){
  267. // true = "handled elsewhere, bypass text extraction"
  268. return true
  269. }
  270. }
  271. margins = {
  272. top: 80,
  273. bottom: 60,
  274. left: 40,
  275. width: 522
  276. };
  277. // all coords and widths are in jsPDF instance's declared units
  278. // 'inches' in this case
  279. pdf.fromHTML(
  280. source // HTML string or DOM elem ref.
  281. , margins.left // x coord
  282. , margins.top // y coord
  283. , {
  284. 'width': margins.width // max width of content on PDF
  285. , 'elementHandlers': specialElementHandlers
  286. },
  287. function (dispose) {
  288. // dispose: object with X, Y of the last line add to the PDF
  289. // this allow the insertion of new lines after html
  290. pdf.save('Test.pdf');
  291. },
  292. margins
  293. )
  294. </pre>
  295. <button onclick="javascript:demoFromHTML()" class="button">Run Code</button></p></div></div>
  296. <h2><a href="#">Text alignment</a></h2>
  297. <div><p><pre>var pdf = new jsPDF('p', 'pt', 'letter');
  298. pdf.text( 'This text is normally\raligned.', 140, 50 );
  299. pdf.text( 'This text is centered\raround\rthis point.', 140, 120, 'center' );
  300. pdf.text( 'This text is rotated\rand centered around\rthis point.', 140, 300, 45, 'center' );
  301. pdf.text( 'This text is\raligned to the\rright.', 140, 400, 'right' );
  302. pdf.text( 'This text is\raligned to the\rright.', 140, 550, 45, 'right' );
  303. pdf.text( 'This single line is centered', 460, 50, 'center' );
  304. pdf.text( 'This right aligned text\r\rhas an empty line.', 460, 200, 'right' );
  305. pdf.save('Test.pdf');</pre>
  306. <a href="javascript:demoTextAlign()" class="button">Run Code</a></p></div>
  307. </div>
  308. </div>
  309. <div id="tabs-graphic">
  310. <div id="accordion-graphic">
  311. <h2><a href="#">Draw example: rectangles / squares</a></h2>
  312. <div><p><pre>var doc = new jsPDF();
  313. doc.rect(20, 20, 10, 10); // empty square
  314. doc.rect(40, 20, 10, 10, 'F'); // filled square
  315. doc.setDrawColor(255,0,0);
  316. doc.rect(60, 20, 10, 10); // empty red square
  317. doc.setDrawColor(255,0,0);
  318. doc.rect(80, 20, 10, 10, 'FD'); // filled square with red borders
  319. doc.setDrawColor(0);
  320. doc.setFillColor(255,0,0);
  321. doc.rect(100, 20, 10, 10, 'F'); // filled red square
  322. doc.setDrawColor(0);
  323. doc.setFillColor(255,0,0);
  324. doc.rect(120, 20, 10, 10, 'FD'); // filled red square with black borders
  325. doc.setDrawColor(0);
  326. doc.setFillColor(255, 255, 255);
  327. doc.roundedRect(140, 20, 10, 10, 3, 3, 'FD'); // Black square with rounded corners
  328. doc.save('Test.pdf');</pre>
  329. <a href="javascript:demoRectangles()" class="button">Run Code</a></p></div>
  330. <h2><a href="#">Draw example: lines</a></h2>
  331. <div><p><pre>var doc = new jsPDF();
  332. doc.line(20, 20, 60, 20); // horizontal line
  333. doc.setLineWidth(0.5);
  334. doc.line(20, 25, 60, 25);
  335. doc.setLineWidth(1);
  336. doc.line(20, 30, 60, 30);
  337. doc.setLineWidth(1.5);
  338. doc.line(20, 35, 60, 35);
  339. doc.setDrawColor(255,0,0); // draw red lines
  340. doc.setLineWidth(0.1);
  341. doc.line(100, 20, 100, 60); // vertical line
  342. doc.setLineWidth(0.5);
  343. doc.line(105, 20, 105, 60);
  344. doc.setLineWidth(1);
  345. doc.line(110, 20, 110, 60);
  346. doc.setLineWidth(1.5);
  347. doc.line(115, 20, 115, 60);
  348. doc.save('Test.pdf');</pre>
  349. <a href="javascript:demoLines()" class="button">Run Code</a></p></div>
  350. <h2><a href="#">Draw example: circles and ellipses</a></h2>
  351. <div><p><pre>var doc = new jsPDF();
  352. doc.ellipse(40, 20, 10, 5);
  353. doc.setFillColor(0,0,255);
  354. doc.ellipse(80, 20, 10, 5, 'F');
  355. doc.setLineWidth(1);
  356. doc.setDrawColor(0);
  357. doc.setFillColor(255,0,0);
  358. doc.circle(120, 20, 5, 'FD');
  359. doc.save('Test.pdf');</pre>
  360. <a href="javascript:demoCircles()" class="button">Run Code</a></p></div>
  361. <h2><a href="#">Draw example: triangles</a></h2>
  362. <div><p><pre>var doc = new jsPDF();
  363. doc.triangle(60, 100, 60, 120, 80, 110, 'FD');
  364. doc.setLineWidth(1);
  365. doc.setDrawColor(255,0,0);
  366. doc.setFillColor(0,0,255);
  367. doc.triangle(100, 100, 110, 100, 120, 130, 'FD');
  368. doc.save('My file.pdf');</pre>
  369. <a href="javascript:demoTriangles()" class="button">Run Code</a></p></div>
  370. <h2><a href="#">Draw example: Images</a></h2>
  371. <div><p><pre>// Because of security restrictions, getImageFromUrl will
  372. // not load images from other domains. Chrome has added
  373. // security restrictions that prevent it from loading images
  374. // when running local files. Run with: chromium --allow-file-access-from-files --allow-file-access
  375. // to temporarily get around this issue.
  376. var getImageFromUrl = function(url, callback) {
  377. var img = new Image();
  378. img.onError = function() {
  379. alert('Cannot load image: "'+url+'"');
  380. };
  381. img.onload = function() {
  382. callback(img);
  383. };
  384. img.src = url;
  385. }
  386. // Since images are loaded asyncronously, we must wait to create
  387. // the pdf until we actually have the image.
  388. // If we already had the jpeg image binary data loaded into
  389. // a string, we create the pdf without delay.
  390. var createPDF = function(imgData) {
  391. var doc = new jsPDF();
  392. // This is a modified addImage example which requires jsPDF 1.0+
  393. // You can check the former one at <em>examples/js/basic.js</em>
  394. doc.addImage(imgData, 'JPEG', 10, 10, 50, 50, 'monkey'); // Cache the image using the alias 'monkey'
  395. doc.addImage('monkey', 70, 10, 100, 120); // use the cached 'monkey' image, JPEG is optional regardless
  396. // As you can guess, using the cached image reduces the generated PDF size by 50%!
  397. // Rotate Image - new feature as of 2014-09-20
  398. doc.addImage({
  399. imageData : imgData,
  400. angle : -20,
  401. x : 10,
  402. y : 78,
  403. w : 45,
  404. h : 58
  405. });
  406. // Output as Data URI
  407. doc.output('datauri');
  408. }
  409. getImageFromUrl('thinking-monkey.jpg', createPDF);
  410. </pre>
  411. <!--a href="javascript:demoImages()" class="button">Run Code</a-->
  412. <!-- I'm lazy, so using eval() directly, sorry ;) [diegocr] -->
  413. <a href="javascript:void(0);" onclick="eval($(this).prev().text())" class="button">Run Code</a>
  414. </p></div>
  415. </div>
  416. </div>
  417. </div>
  418. </div>
  419. </body>
  420. </html>