Commit 838437c9 authored by Fred Chasen's avatar Fred Chasen
Browse files

Setup puppeteer

parent 85717752
Pipeline #1 canceled with stages
The MIT License (MIT)
Copyright (c) 2018 Adam Hyde
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var index = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
#!/usr/bin/env node
const Paged = require('pagedjs');
const puppeteer = require('puppeteer');
const program = require('commander');
// const temp = require("temp").track();
const path = require('path');
const fs = require('fs');
const replaceExt = require('replace-ext');
const express = require('express');
const app = express();
const PORT = 9999;
program
.version(require('../package.json').version)
.arguments('[inputPath]')
.option('-i, --inputs [inputs]', 'Inputs')
.option('-o, --output [output]', 'Output')
.option('-d, --debug', 'Debug')
.option('-l, --landscape', 'Landscape printing', false)
.option('-s, --page-size [size]', 'Print to Page Size [size]')
.option('-w, --width [size]', 'Print to Page Width [width] in MM')
.option('-h --height [size]', 'Print to Page Height [weight] in MM')
// .option('-m, --page-margin [margin]', 'Print with margin [margin]')
// .option('-n, --hyphenate [lang]', 'Hyphenate with language [language], defaults to "en-us"')
// .option('-hi, --hypher_ignore [str]', 'Ignore passed element selectors, such as ".class_to_ignore, h1"')
// .option('-ho, --hypher_only [str]', 'Only hyphenate passed elements selector, such as ".hyphenate, aside"')
// .option('-e, --encoding [type]', 'Set the encoding of the input html, defaults to "utf-8"')
.option('-t, --timeout [ms]', 'Set a max timeout of [ms]')
.parse(process.argv);
let input = program.inputs || program.args[0];
let dir = process.cwd();
let relativePath = path.resolve(dir, input);
let output;
let tmpFile, tmpPath;
// var hyphenator;
// var hyphenateOptions;
if (!input) {
console.error("You must include an input path");
return process.exit(1);
}
if (['.html', '.xhtml'].indexOf(path.extname(relativePath)) === -1) {
console.error("Must pass a html or xhtml file as input");
return process.exit(1);
}
try {
fs.accessSync(relativePath, fs.F_OK);
} catch (e) {
console.error("Input cannot be found", e);
return process.exit(1);
}
if (typeof(program.output) === "string") {
output = path.resolve(dir, program.output);
} else if (typeof(program.output) !== "undefined") {
output = './' + replaceExt(path.basename(input), '.pdf');
} else {
output = "output.pdf";
}
console.log("output", output, program.output);
/*
if (program.hyphenate) {
hyphenateOptions = {
ignore: program.hypher_ignore || undefined,
only: program.hypher_only || undefined,
encoding: program.encoding || undefined
}
tmpPath = replaceExt(relativePath, ".hyphenated.html");
// tmpFile = temp.openSync({suffix: '.html'});
// tmpPath = tmpFile.path;
// Create a new Hyphenator, with passed language
hyphenator = new Hyphenator(program.hyphenate);
hyphenator.process(relativePath, tmpPath, hyphenateOptions);
console.log("Hyphenated for", typeof(program.hyphenate) === "string" ? program.hyphenate : "en-us");
if (program.debug && tmpPath) {
console.log("Hyphenated file at:", tmpPath);
}
}
*/
(async () => {
const browser = await puppeteer.launch({
// headless: false,
// args: ['--no-sandbox', '--allow-file-access-from-files', '--enable-local-file-accesses']
});
const page = await browser.newPage();
let relativePath = path.resolve(dir, input);
let dirname = path.dirname(relativePath);
let basename = path.basename(relativePath);
app.use("/print", express.static(dirname))
let scriptPath = path.resolve(dir, "./node_modules/pagedjs/dist/");
app.use("/polyfill", express.static(scriptPath))
let server = app.listen(PORT);
await page.goto(`http://localhost:${PORT}/print/${basename}`);
await page.addScriptTag({
url: `http://localhost:${PORT}/polyfill/paged.polyfill.js`
});
await page.exposeFunction('onPagesRendered', async (msg, width, height, orientation) => {
console.log(msg);
console.log("Saved to", output);
let pdf = await page.pdf({
path: output,
printBackground: true,
displayHeaderFooter: false,
width: width,
height: height,
orientation: orientation,
margin: {
top: 0,
right: 0,
bottom: 0,
left: 0,
},
// format: 'A4'
}).catch((e) => {
console.error(e);
})
server.close();
await browser.close();
});
})();
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('demo:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
This diff is collapsed.
{
"name": "demo",
"version": "0.0.0",
"private": true,
"name": "pagedjs-cli",
"version": "0.0.1",
"author": "Fred Chasen",
"license": "MIT",
"homepage": "https://pagedmedia.org",
"repository": {
"type": "git",
"url": "https://gitlab.pagedmedia.org/polyfills/pagedjs-cli.git"
},
"scripts": {
"start": "node ./bin/www"
"start": "./bin/paged"
},
"dependencies": {
"body-parser": "~1.17.1",
"cookie-parser": "~1.4.3",
"debug": "~2.6.3",
"express": "~4.15.2",
"jade": "~1.11.0",
"morgan": "~1.8.1",
"serve-favicon": "~2.4.2"
"commander": "^2.15.1",
"express": "^4.16.3",
"pagedjs": "0.0.1",
"puppeteer": "^1.2.0",
"replace-ext": "^1.0.0"
}
}
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}
a {
color: #00B7FF;
}
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
module.exports = router;
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('respond with a resource');
});
module.exports = router;
# Aurorae
Test of different generating PDF tools using HTML and CSS.
* PDF reactor : [http://www.pdfreactor.com/](http://www.pdfreactor.com/)
* Prince XML : [https://www.princexml.com/](https://www.princexml.com/)
## Book
The book "Auroræ : their characters and spectra" by J. Rand Capron is used for comparaison.
The original (x)HTML file is part of the[Gutenberg project](http://www.gutenberg.org/), it has been transformed for the needs.
[I'm an inline-style link](http://www.gutenberg.org/ebooks/56159?msg=welcome_stranger)
Licence : Project Gutenberg License
Title: Auroræ: Their Characters and Spectra
Release Date: December 10, 2017 [EBook #56159]
Language: English
Character set encoding: UTF-8
## Files
`index.html` : the Auroræ book
Before using, you must change the path of the CSS and javascript files according to the tool tested.
`book.css` : commun CSS for all tools (styling + footnotes + page breaks)
`fonts` and `images` folders contain the resources used in all tools
Each tools have a specific file with :
* `layout-*.html` : CSS contain features for layout
* `script.html` : most tools accept javascript with proprietary access to layout information
* `aurorae-*.pdf` : the PDF generated
## TODO
* add other test-tools
* Vivliostyle
* Antennahouse
* add table comparaison
* add images, plates and figures + list of figures
* add index
* add cross references
* Styling tables
* all table in chapters
* add border-bottom when table is break
* #table-appa-01 > 2 column
* add mathematical support + Styling
* balancing text in titles
* clean HTML
* add span Greek
* transform sidenotes into span
* remove all !important in CSS
html{
--serif: 'spectral', serif;
--sans-serif: 'hk-grotesk', sans-serif;
--font-size: 13px;
--font-size-notes: 9px;
--line-height: 15px;
--line-height-notes: calc(15px*2/3);
--indent: 6mm;
--baseline: 15px;
}
@page {
size: 148mm 210mm portait;
}
@media screen {
.page {
background-image: repeating-linear-gradient(180deg, transparent 0, transparent 14px , rgba(0,255,0,0.7) var(--baseline)) ;
background-size: cover;
background-position: 0 -3px , 0 0
}
}
@page cover {
margin: 0;
background: url("images/cover.jpg");
background-position: center;
background-size: 104%;
background-repeat: no-repeat;
@top-left{ content: none; }
@top-center{ content: none; }
@top-right{ content: none; }
@bottom-left{ content: none; }
}
@page:blank {
@top-left-corner{ content: none; }
@top-left{ content: none; }
@top-center{ content: none; }
@top-right{ content: none; }
@top-right-corner{ content: none; }
@right-top{ content: none; }
@right-middle{ content: none; }
@right-bottom{ content: none; }
@bottom-right-corner{ content: none; }
@bottom-right{ content: none; }
@bottom-center{ content: none; }
@bottom-left{ content: none; }
@bottom-left-corner{ content: none; }
@left-bottom{ content: none; }
@left-middle{ content: none; }
@left-top{ content: none; }
}
@page:left {
margin-left:22mm;
margin-right:14mm;
margin-top: 73px;
margin-bottom: 73px;
@top-left{
content: counter(page);
font-weight: 400;
font-family: var(--serif);
font-size: var(--font-size);
font-weight: normal;
line-height: var(--line-height);
vertical-align: bottom;
margin-bottom: 14px;
font-variant-numeric: oldstyle-nums;
}
@top-center{
content: string(booktitle);
font-weight: 400;
font-family: var(--serif);
font-size: var(--font-size);
font-weight: normal;
line-height: var(--line-height);
vertical-align: bottom;
margin-bottom: 14px;
font-variant-numeric: oldstyle-nums;
font-variant: small-caps;
width: 85mm;
text-align: left;
}
}
@page:right {
margin-left:14mm;
margin-right:22mm;
margin-top: 73px;
margin-bottom: 73px;
@top-right{
content: counter(page);
font-weight: 400;
font-family: var(--serif);
font-size: var(--font-size);
font-weight: normal;
line-height: var(--line-height);
vertical-align: bottom;
margin-bottom: 14px;
font-variant-numeric: oldstyle-nums;
text-align: right;
}
@top-center{
content: element(shorter);
vertical-align: bottom;
text-align: right;
margin-bottom: 1px;
width: 85mm;
}
}
.sidenote, .footnote {
display:none!important;
}
.footnote::footnote-call, .footnote::footnote-marker{
display:none!important;
}
.part h2, #appendices h2,
section[data-type="half-title"],
section[data-type="titlepage"],
section[data-type="copyright"],
section[data-type="dedication"],
section[data-type="epigraph"],
#toc {
margin-left: 10mm /*center the page*/
}
.part h2, #appendices h2{
page: part;
}
/* running elements */
#cover h1 {
string-set: booktitle content(text);
}
#cover {
page: cover;
}
.shorter{
position: running(shorter);
font-weight: 400;
font-family: var(--serif);
font-size: var(--font-size);
font-weight: normal;
line-height: var(--line-height);
text-align: right;
/* font-style: italic; */
font-variant-numeric: oldstyle-nums;
}
/* flexbox */
section[data-type="copyright"] #flexbox {
display: block;
position: absolute;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
left:0;
}
/* table of content */
#toc{
counter-reset: chaptoc apptoc;
}
.toc-part a[href]::before{
content: target-text(attr(href), before);
}