html2pdf_magic_word_new.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. "use strict";
  2. var pdf_mingyan = require('./pdf_mingyan.js');
  3. var page = require('webpage').create(),
  4. system = require('system'),
  5. address, output, size, t, title, getFooter, getHeader, totalPage;
  6. //设置资源价值超时时间(10s)
  7. page.settings.resourceTimeout = 20000
  8. var pageTileObj = JSON.parse(system.args[4]);
  9. //学科
  10. var subject_id = 3;
  11. if (pageTileObj.subject_id !== undefined) {
  12. subject_id = pageTileObj.subject_id;
  13. }
  14. //词汇宝
  15. var mw_card = 0;
  16. if (pageTileObj.mw_card !== undefined) {
  17. mw_card = pageTileObj.mw_card;
  18. }
  19. var address_magic_word = system.args[1];
  20. getFooter = function (pageNum, numPages) {
  21. totalPage = numPages;
  22. // return "<table style=\"border-top: 1px solid; width: 95%; margin: 0 auto; font-family: SimSun; font-size: 9px;\">" +
  23. // "<tr>" +
  24. // "<td style=\"text-align: left;\">" + pdf_mingyan.getMingyan(subject_id) + "</td>" +
  25. // "<td style=\"text-align: right;\">第<span class=\"page\">" + pageNum + "</span>页,共<span class=\"topage\">" + numPages + "</span>页</td>" +
  26. // "</tr>" +
  27. // "</table>";
  28. if (mw_card == 1 && (address_magic_word.indexOf("card") !== -1 || address_magic_word.indexOf("Card") !== -1)) {
  29. // 英语检测卡页脚
  30. return "<div style=\"width: 100%; text-align: center; font-family: Times New Roman,SimSun; font-size: 10px; position: relative;\">" +
  31. "<div style=\"width: 100%;text-align: center; position: absolute; top: -9px;\">第<span class=\"page\">" + pageNum + "</span>页,共<span class=\"topage\">" + totalPage + "</span>页</div>" +
  32. "</div>"
  33. }
  34. return "<div style=\"border-top: 1px solid; width: 95%; height: 100%; margin: 0 auto; padding-top: 2px; overflow: hidden; font-family: Times New Roman,SimSun; font-size: 9px;\">" +
  35. "<div style=\"float: left; max-width: 85%; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;\">" + pdf_mingyan.getMingyan(subject_id) + "</div>" +
  36. "<div style=\"float: right;\">第<span class=\"page\">" + pageNum + "</span>页,共<span class=\"topage\">" + numPages + "</span>页</div>" +
  37. "</div>";
  38. }
  39. getHeader = function (pageTileObj) {
  40. var title_left, title_mid, title_right, header_td_html;
  41. title_left = '';
  42. title_mid = '';
  43. title_right = '';
  44. if (pageTileObj.student_name !== undefined) {
  45. title_left = title_left + "<span style=\"font-size: 12px; font-weight: bold;\">" + pageTileObj.student_name +"</span>" + ' ';
  46. }
  47. if (pageTileObj.school_name !== undefined) {
  48. title_left = title_left + pageTileObj.school_name + ' ';
  49. }
  50. if (pageTileObj.class_name !== undefined) {
  51. title_left = title_left + pageTileObj.class_name + ' ';
  52. }
  53. if (pageTileObj.student_card !== undefined) {
  54. title_left = title_left + '准考证号:' + pageTileObj.student_card + ' ';
  55. }
  56. if (pageTileObj.exam_name !== undefined) {
  57. title_mid = pageTileObj.exam_name + ' ';
  58. } else {
  59. title_mid = '';
  60. }
  61. if (pageTileObj.exam_date !== undefined) {
  62. title_right = '时间:' + pageTileObj.exam_date;
  63. }
  64. if (pageTileObj.product_type_name !== undefined) {
  65. title_right = title_right + ' ' + pageTileObj.product_type_name;
  66. }
  67. // header_td_html = "<td style=\"text-align: left;font-size: 10px;\">" + title_left + "</td><td style=\"text-align:center;\">" + title_mid + "</td>" +
  68. // "<td style=\"text-align: right;\">" + title_right + "</td>";
  69. // return "<table style=\"border-bottom: 1px solid black; width: 470px;font-family: Arial;font-size: 8px;\">" +
  70. // "<tr>" +
  71. // header_td_html +
  72. // "</tr>" +
  73. // "</table>";
  74. if (mw_card == 1 && (address_magic_word.indexOf("card") !== -1 || address_magic_word.indexOf("Card") !== -1)) {
  75. // 英语检测卡页头
  76. return '<div></div>'
  77. }
  78. header_td_html = "<div style=\"position: absolute; left: 0; bottom: 0;\">" + title_left + "</div>" +
  79. "<div style=\"width: 100%; text-align: center; position: absolute; bottom: 0;\">" + title_mid + "</div>" +
  80. "<div style=\"position: absolute; right: 0; bottom: 0;\">" + title_right + "</div>";
  81. return "<div style=\"border-bottom: 1px solid; width: 95%; height: 100%; margin: 0 auto; font-family: SimSun; font-size: 9px; position: relative;\">" +
  82. header_td_html +
  83. "</div>";
  84. }
  85. if (!system.args.length === 5) {
  86. console.log('Usage: html2pdf.js URL filename [paperwidth*paperheight|paperformat] title [subjectId]');
  87. console.log(' paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "176mm*250mm", "A4", "Letter"');
  88. phantom.exit();
  89. } else {
  90. address = system.args[1];
  91. output = system.args[2];
  92. title = system.args[4];
  93. page.viewportSize = {
  94. width: 600,
  95. height: 600
  96. };
  97. if (system.args[2].substr(-4) === ".pdf") {
  98. size = system.args[3].split('*');
  99. if (size.length === 2) {
  100. var margin = "10px";
  101. if (mw_card == 1) {
  102. margin = "0px"
  103. }
  104. page.paperSize = {
  105. width: size[0],
  106. height: size[1],
  107. margin: margin,
  108. header: {
  109. height: "1cm",
  110. contents: phantom.callback(function (pageNum, numPages) {
  111. return getHeader(pageTileObj);
  112. })
  113. },
  114. footer: {
  115. height: "1cm",
  116. contents: phantom.callback(function (pageNum, numPages) {
  117. return getFooter(pageNum, numPages);
  118. })
  119. }
  120. }
  121. } else {
  122. page.paperSize = {
  123. format: system.args[3],
  124. orientation: 'portrait',
  125. margin: '10px',
  126. header: {
  127. height: "1cm",
  128. contents: phantom.callback(function (pageNum, numPages) {
  129. return getHeader(pageTileObj);
  130. })
  131. },
  132. footer: {
  133. height: "1cm",
  134. contents: phantom.callback(function (pageNum, numPages) {
  135. return getFooter(pageNum, numPages);
  136. })
  137. }
  138. }
  139. }
  140. } else {
  141. console.log('Wrong format!');
  142. phantom.exit();
  143. }
  144. t = Date.now();
  145. page.open(address, function (status) {
  146. if (status !== 'success') {
  147. console.log('Unable to load URL!');
  148. phantom.exit();
  149. } else {
  150. window.setTimeout(function () {
  151. page.render(output);
  152. t = Date.now() - t;
  153. console.log('Generate PDF file succeed in ' + t + ' msec!');
  154. console.log('##' + totalPage + '##');
  155. phantom.exit();
  156. }, 200);
  157. }
  158. });
  159. }