renderPDF.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. ;(function(global){
  2. var pdfFormat = {
  3. 'a0': [2383.94, 3370.39],
  4. 'a1': [1683.78, 2383.94],
  5. 'a2': [1190.55, 1683.78],
  6. 'a3': [841.89, 1190.55],
  7. 'a4': [595.28, 841.89],
  8. 'a5': [419.53, 595.28],
  9. 'a6': [297.64, 419.53],
  10. 'a7': [209.76, 297.64],
  11. 'a8': [147.40, 209.76],
  12. 'a9': [104.88, 147.40],
  13. 'a10': [73.70, 104.88],
  14. 'b0': [2834.65, 4008.19],
  15. 'b1': [2004.09, 2834.65],
  16. 'b2': [1417.32, 2004.09],
  17. 'b3': [1000.63, 1417.32],
  18. 'b4': [708.66, 1000.63],
  19. 'b5': [498.90, 708.66],
  20. 'b6': [354.33, 498.90],
  21. 'b7': [249.45, 354.33],
  22. 'b8': [175.75, 249.45],
  23. 'b9': [124.72, 175.75],
  24. 'b10': [87.87, 124.72],
  25. 'c0': [2599.37, 3676.54],
  26. 'c1': [1836.85, 2599.37],
  27. 'c2': [1298.27, 1836.85],
  28. 'c3': [918.43, 1298.27],
  29. 'c4': [649.13, 918.43],
  30. 'c5': [459.21, 649.13],
  31. 'c6': [323.15, 459.21],
  32. 'c7': [229.61, 323.15],
  33. 'c8': [161.57, 229.61],
  34. 'c9': [113.39, 161.57],
  35. 'c10': [79.37, 113.39],
  36. 'dl': [311.81, 623.62],
  37. 'letter': [612, 792],
  38. 'government-letter': [576, 756],
  39. 'legal': [612, 1008],
  40. 'junior-legal': [576, 360],
  41. 'ledger': [1224, 792],
  42. 'tabloid': [792, 1224]
  43. }
  44. // the width and height of content decided by the format of pdf
  45. // 'a0': [2383.94, 3370.39], 'a1': [1683.78, 2383.94],
  46. // 'a2': [1190.55, 1683.78], 'a3': [841.89, 1190.55],
  47. // 'a4': [595.28, 841.89], 'a5': [419.53, 595.28],
  48. // 'a6': [297.64, 419.53], 'a7': [209.76, 297.64],
  49. // 'a8': [147.40, 209.76], 'a9': [104.88, 147.40],
  50. // 'a10': [73.70, 104.88], 'b0': [2834.65, 4008.19],
  51. // 'b1': [2004.09, 2834.65], 'b2': [1417.32, 2004.09],
  52. // 'b3': [1000.63, 1417.32], 'b4': [708.66, 1000.63],
  53. // 'b5': [498.90, 708.66], 'b6': [354.33, 498.90],
  54. // 'b7': [249.45, 354.33], 'b8': [175.75, 249.45],
  55. // 'b9': [124.72, 175.75], 'b10': [87.87, 124.72],
  56. // 'c0': [2599.37, 3676.54], 'c1': [1836.85, 2599.37],
  57. // 'c2': [1298.27, 1836.85], 'c3': [918.43, 1298.27],
  58. // 'c4': [649.13, 918.43], 'c5': [459.21, 649.13],
  59. // 'c6': [323.15, 459.21], 'c7': [229.61, 323.15],
  60. // 'c8': [161.57, 229.61], 'c9': [113.39, 161.57],
  61. // 'c10': [79.37, 113.39], 'dl': [311.81, 623.62],
  62. // 'letter': [612, 792],
  63. // 'government-letter': [576, 756],
  64. // 'legal': [612, 1008],
  65. // 'junior-legal': [576, 360],
  66. // 'ledger': [1224, 792],
  67. // 'tabloid': [792, 1224]
  68. global.renderPDF = function(content, pdfName, format, onSuccess){
  69. pdfName = pdfName || "content";
  70. if(pdfName.indexOf(".pdf") < 0){
  71. pdfName = pdfName + ".pdf";
  72. }
  73. format = format || "a4";
  74. var copyContent = content.cloneNode();
  75. copyContent.innerHTML = content.innerHTML;
  76. copyContent.style.position = "absolute";
  77. copyContent.style.zIndex = "-999";
  78. copyContent.style.top = "2000px";
  79. content = copyContent;
  80. document.body.appendChild(content);
  81. var pdfProportion = pdfFormat[format][0] / pdfFormat[format][1];
  82. var contentHeight = content.offsetHeight;
  83. var contentWidth = content.offsetWidth;
  84. var pageHeight = contentWidth / pdfProportion;
  85. var pageWidth = contentWidth;
  86. function renderPages(imgData){
  87. var pdf = new jsPDF("p", "pt", format);
  88. if(contentHeight < pageHeight){
  89. pdf.addImage(imgData, 'JPEG', 0, 0,pdfFormat[format][0],pdfFormat[format][1]/pageHeight*contentHeight);
  90. pdf.save(pdfName);
  91. }
  92. else{
  93. var index = 0;
  94. var count = parseInt(contentHeight / pageHeight);
  95. var page = document.createElement("div");
  96. page.style.position = "absolute";
  97. page.style.width = contentWidth + "px";
  98. page.style.height = pageHeight + "px";
  99. page.style.backgroundImage = "url(" + imgData + ")";
  100. page.style.backgroundRepeat = "norepeat";
  101. document.body.appendChild(page);
  102. function addPage(i, onFinished){
  103. page.style.backgroundPositionY = -pageHeight * i + "px";
  104. html2canvas(page, {
  105. onrendered: function(canvas) {
  106. var pageData = canvas.toDataURL('image/jpeg');
  107. pdf.addImage(pageData, 'JPEG', 0, 0,pdfFormat[format][0],pdfFormat[format][1]);
  108. if(i + 1 < count){
  109. pdf = pdf.addPage();
  110. addPage(i + 1, onFinished);
  111. }
  112. else{
  113. onFinished()
  114. }
  115. }
  116. });
  117. }
  118. addPage(index, function(){
  119. page.style.backgroundPositionY = -pageHeight * count + "px";
  120. var lastPageHeight = contentHeight % pageHeight;
  121. page.style.height = lastPageHeight + "px";
  122. html2canvas(page, {
  123. onrendered: function(canvas) {
  124. var pageData = canvas.toDataURL('image/jpeg');
  125. pdf = pdf.addPage();
  126. pdf.addImage(pageData, 'JPEG', 0, 0,pdfFormat[format][0], pdfFormat[format][1]/pageHeight*lastPageHeight);
  127. document.body.removeChild(page);
  128. document.body.removeChild(content);
  129. onSuccess && onSuccess();
  130. pdf.save(pdfName);
  131. }
  132. });
  133. });
  134. }
  135. }
  136. html2canvas(content, {
  137. // 渲染完成时调用,获得 canvas
  138. onrendered: function(canvas) {
  139. var imgData = canvas.toDataURL('image/jpeg');
  140. renderPages(imgData);
  141. }
  142. });
  143. }
  144. })(window);