123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- "use strict";
- var pdf_mingyan = require('./pdf_mingyan.js');
- var page = require('webpage').create(),
- system = require('system'),
- address, output, size, t, title, getFooter, getHeader, totalPage;
- //设置资源价值超时时间(10s)
- page.settings.resourceTimeout = 20000
- var pageTileObj = JSON.parse(system.args[4]);
- //学科
- var subject_id = 3;
- if (pageTileObj.subject_id !== undefined) {
- subject_id = pageTileObj.subject_id;
- }
- //词汇宝
- var mw_card = 0;
- if (pageTileObj.mw_card !== undefined) {
- mw_card = pageTileObj.mw_card;
- }
- var address_magic_word = system.args[1];
- getFooter = function (pageNum, numPages) {
- totalPage = numPages;
- // return "<table style=\"border-top: 1px solid; width: 95%; margin: 0 auto; font-family: SimSun; font-size: 9px;\">" +
- // "<tr>" +
- // "<td style=\"text-align: left;\">" + pdf_mingyan.getMingyan(subject_id) + "</td>" +
- // "<td style=\"text-align: right;\">第<span class=\"page\">" + pageNum + "</span>页,共<span class=\"topage\">" + numPages + "</span>页</td>" +
- // "</tr>" +
- // "</table>";
- if (mw_card == 1 && (address_magic_word.indexOf("card") !== -1 || address_magic_word.indexOf("Card") !== -1)) {
- // 英语检测卡页脚
- return "<div style=\"width: 100%; text-align: center; font-family: Times New Roman,SimSun; font-size: 10px; position: relative;\">" +
- "<div style=\"width: 100%;text-align: center; position: absolute; top: -9px;\">第<span class=\"page\">" + pageNum + "</span>页,共<span class=\"topage\">" + totalPage + "</span>页</div>" +
- "</div>"
- }
- 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;\">" +
- "<div style=\"float: left; max-width: 85%; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;\">" + pdf_mingyan.getMingyan(subject_id) + "</div>" +
- "<div style=\"float: right;\">第<span class=\"page\">" + pageNum + "</span>页,共<span class=\"topage\">" + numPages + "</span>页</div>" +
- "</div>";
- }
- getHeader = function (pageTileObj) {
- var title_left, title_mid, title_right, header_td_html;
- title_left = '';
- title_mid = '';
- title_right = '';
- if (pageTileObj.student_name !== undefined) {
- title_left = title_left + "<span style=\"font-size: 12px; font-weight: bold;\">" + pageTileObj.student_name +"</span>" + ' ';
- }
- if (pageTileObj.school_name !== undefined) {
- title_left = title_left + pageTileObj.school_name + ' ';
- }
- if (pageTileObj.class_name !== undefined) {
- title_left = title_left + pageTileObj.class_name + ' ';
- }
- if (pageTileObj.student_card !== undefined) {
- title_left = title_left + '准考证号:' + pageTileObj.student_card + ' ';
- }
- if (pageTileObj.exam_name !== undefined) {
- title_mid = pageTileObj.exam_name + ' ';
- } else {
- title_mid = '';
- }
- if (pageTileObj.exam_date !== undefined) {
- title_right = '时间:' + pageTileObj.exam_date;
- }
- if (pageTileObj.product_type_name !== undefined) {
- title_right = title_right + ' ' + pageTileObj.product_type_name;
- }
- // header_td_html = "<td style=\"text-align: left;font-size: 10px;\">" + title_left + "</td><td style=\"text-align:center;\">" + title_mid + "</td>" +
- // "<td style=\"text-align: right;\">" + title_right + "</td>";
- // return "<table style=\"border-bottom: 1px solid black; width: 470px;font-family: Arial;font-size: 8px;\">" +
- // "<tr>" +
- // header_td_html +
- // "</tr>" +
- // "</table>";
- if (mw_card == 1 && (address_magic_word.indexOf("card") !== -1 || address_magic_word.indexOf("Card") !== -1)) {
- // 英语检测卡页头
- return '<div></div>'
- }
- header_td_html = "<div style=\"position: absolute; left: 0; bottom: 0;\">" + title_left + "</div>" +
- "<div style=\"width: 100%; text-align: center; position: absolute; bottom: 0;\">" + title_mid + "</div>" +
- "<div style=\"position: absolute; right: 0; bottom: 0;\">" + title_right + "</div>";
- return "<div style=\"border-bottom: 1px solid; width: 95%; height: 100%; margin: 0 auto; font-family: SimSun; font-size: 9px; position: relative;\">" +
- header_td_html +
- "</div>";
- }
- if (!system.args.length === 5) {
- console.log('Usage: html2pdf.js URL filename [paperwidth*paperheight|paperformat] title [subjectId]');
- console.log(' paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "176mm*250mm", "A4", "Letter"');
- phantom.exit();
- } else {
- address = system.args[1];
- output = system.args[2];
- title = system.args[4];
- page.viewportSize = {
- width: 600,
- height: 600
- };
- if (system.args[2].substr(-4) === ".pdf") {
- size = system.args[3].split('*');
- if (size.length === 2) {
- var margin = "10px";
- if (mw_card == 1) {
- margin = "0px"
- }
- page.paperSize = {
- width: size[0],
- height: size[1],
- margin: margin,
- header: {
- height: "1cm",
- contents: phantom.callback(function (pageNum, numPages) {
- return getHeader(pageTileObj);
- })
- },
- footer: {
- height: "1cm",
- contents: phantom.callback(function (pageNum, numPages) {
- return getFooter(pageNum, numPages);
- })
- }
- }
- } else {
- page.paperSize = {
- format: system.args[3],
- orientation: 'portrait',
- margin: '10px',
- header: {
- height: "1cm",
- contents: phantom.callback(function (pageNum, numPages) {
- return getHeader(pageTileObj);
- })
- },
- footer: {
- height: "1cm",
- contents: phantom.callback(function (pageNum, numPages) {
- return getFooter(pageNum, numPages);
- })
- }
- }
- }
- } else {
- console.log('Wrong format!');
- phantom.exit();
- }
- t = Date.now();
- page.open(address, function (status) {
- if (status !== 'success') {
- console.log('Unable to load URL!');
- phantom.exit();
- } else {
- window.setTimeout(function () {
- page.render(output);
- t = Date.now() - t;
- console.log('Generate PDF file succeed in ' + t + ' msec!');
- console.log('##' + totalPage + '##');
- phantom.exit();
- }, 200);
- }
- });
- }
|