"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 "" + // "" + // "" + // "" + // "" + // "
" + pdf_mingyan.getMingyan(subject_id) + "" + pageNum + "页,共" + numPages + "
"; if (mw_card == 1 && (address_magic_word.indexOf("card") !== -1 || address_magic_word.indexOf("Card") !== -1)) { // 英语检测卡页脚 return "
" + "
" + pageNum + "页,共" + totalPage + "
" + "
" } return "
" + "
" + pdf_mingyan.getMingyan(subject_id) + "
" + "
" + pageNum + "页,共" + numPages + "
" + "
"; } 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 + "" + pageTileObj.student_name +"" + ' '; } 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 = "" + title_left + "" + title_mid + "" + // "" + title_right + ""; // return "" + // "" + // header_td_html + // "" + // "
"; if (mw_card == 1 && (address_magic_word.indexOf("card") !== -1 || address_magic_word.indexOf("Card") !== -1)) { // 英语检测卡页头 return '
' } header_td_html = "
" + title_left + "
" + "
" + title_mid + "
" + "
" + title_right + "
"; return "
" + header_td_html + "
"; } 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); } }); }