Commit f4307770 authored by Fred Chasen's avatar Fred Chasen

init

parents
Pipeline #243 failed with stages
in 5 seconds
node_modules
.DS_Store
This diff is collapsed.
{
"name": "pagedjs-server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node src/app.js"
},
"author": "",
"license": "MIT",
"dependencies": {
"kcors": "2.2.2",
"koa": "^2.7.0",
"koa-body": "^4.1.0",
"koa-bodyparser": "4.2.1",
"koa-helmet": "4.1.0",
"koa-logger": "3.2.0",
"koa-mount": "^4.0.0",
"koa-router": "7.4.0",
"koa-validate": "1.0.7",
"logger": "0.0.1",
"node-fetch": "^2.4.1",
"pagedjs": "^0.1.34",
"pagedjs-cli": "0.0.3",
"puppeteer": "^1.15.0"
}
}
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
const PORT = 3000;
const Koa = require('koa');
const Router = require('koa-router');
const cors = require('kcors');
const helmet = require('koa-helmet');
const bodyParser = require('koa-bodyparser');
// const koaBody = require('koa-body');
const requestLogger = require('koa-logger');
const validate = require('koa-validate');
const mount = require('koa-mount');
const pdf = require('./pdf');
const printer = require('./printer');
const app = new Koa();
const router = new Router();
router.post('/printer', printer);
app.use(cors());
app.use(helmet());
app.use(requestLogger());
app.use(bodyParser(
{
enableTypes: ['json', 'form', 'text', 'text/plain', "text/html", "application/xhtml+xml"],
jsonLimit: '5GB',
textLimit: '5GB',
strict: false
},
));
validate(app);
// app.use(ctx => {
// console.log(ctx.request.body);
// });
app.use(router.routes())
app.listen(PORT, () => {
console.warn(`Printer server listening on ${PORT}, in ${process.env.NODE_ENV} mode`);
});
module.exports = app;
const fs = require('fs');
const puppeteer = require('puppeteer');
module.exports = async (ctx) => {
ctx.type = 'application/pdf';
ctx.attachment('out.pdf');
const { url, html, options, headers } = ctx.request.body;
console.debug('URL: ', url);
console.debug('HTML: ', html);
console.debug('OPTIONS: ', options);
console.debug('HEADERS: ', headers);
const browser = await puppeteer.launch({
ignoreHTTPSErrors: true,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage', // https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md#tips
],
});
try {
const page = await browser.newPage();
if (headers && Object.keys(headers).length) {
await page.setExtraHTTPHeaders(headers);
}
await page.goto(url || `data:text/html,${html}`);
await page.addScriptTag({
path: __dirname + "/../../../node_modules/pagedjs/dist/paged.polyfill.js"
});
let width, height, orientation;
let resolveReady;
let ready = new Promise(function(resolve, reject) {
resolveReady = resolve;
});
await page.exposeFunction('onPagesRendered', async (msg, pWidth, pHeight, pOrientation) => {
console.log(msg, pWidth, pHeight, pOrientation);
width = pWidth;
height = pHeight;
orientation = pOrientation;
resolveReady();
})
await ready;
ctx.body = await page.pdf(
Object.assign({
printBackground: true,
displayHeaderFooter: false,
width: width,
height: height,
orientation: orientation,
margin: {
top: 0,
right: 0,
bottom: 0,
left: 0,
}
}, options),
);
await page.close();
} catch (e) {
await closeBrowser(browser);
throw e;
}
};
const fs = require('fs');
const Printer = require('pagedjs-cli');
module.exports = async (ctx) => {
ctx.type = 'application/pdf';
ctx.attachment('out.pdf');
const { url, html, options, headers } = ctx.request.body;
//
// console.debug('URL: ', url);
// console.debug('HTML: ', html);
// console.debug('OPTIONS: ', options);
// console.debug('HEADERS: ', headers);
let printer = new Printer();
printer.on("page", (page) => {
console.debug("Rendering: Page " + (page.position + 1))
});
printer.on("rendered", (msg) => {
console.debug("Generating");
});
printer.on("postprocessing", (msg) => {
console.debug("Processing");
});
let file;
if (url) {
file = await printer.pdf(url, options);
} else if (html) {
file = await printer.pdf({ html }, options);
} else {
file = await printer.pdf({ html: ctx.request.body});
}
ctx.body = Buffer.from(file);
};
const fetch = require("node-fetch");
const util = require('util');
const fs = require('fs');
const readFile = util.promisify(fs.readFile);
(async () => {
const html = await readFile("index.html", "utf-8");
let file = await fetch('http://localhost:3000/printer', {
method: 'POST',
body: JSON.stringify({
options: {},
// url: "http://localhost:9090/examples/assets/ruby.html",
html: html
}),
headers: {
'Content-Type': 'application/json',
},
// responseType: 'blob',
})
.then(res => {
const dest = fs.createWriteStream('./out.pdf');
res.body.pipe(dest);
});
})();
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment