Pmiazga has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/405062 )
Change subject: WIP: Allow injecting extra styles
......................................................................
WIP: Allow injecting extra styles
Bug: T181680
Change-Id: I88720beba4fc13b8c65daeca5347c851b0c53cc4
---
M config.dev.yaml
A lib/pdf-format.js
M lib/queue.js
M lib/renderer.js
A magic.css
M routes/html2pdf-v1.js
6 files changed, 77 insertions(+), 12 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/chromium-render
refs/changes/62/405062/1
diff --git a/config.dev.yaml b/config.dev.yaml
index e2fae85..17fd4f7 100644
--- a/config.dev.yaml
+++ b/config.dev.yaml
@@ -75,16 +75,28 @@
query: '{{ default(request.query, {}) }}'
headers: '{{request.headers}}'
body: '{{request.body}}'
+ document_options:
+ supported_formats:
+ # Each supported format can override an option from pdf_options
section (format, landscape, margin, etc)
+ # See
https://github.com/GoogleChrome/puppeteer/blob/v0.13.0/docs/api.md#pagepdfoptions
for more options
+ a4:
+ format: 'A4'
+ letter:
+ format: 'Letter'
+ legal:
+ format: 'Legal'
+ supported_styles:
+ # Each supported style points to a MediaWiki Resource loader
+ mobile: 'magic.css'
#resources/skins.minerva.base.styles/print/styles.less'
#
https://github.com/GoogleChrome/puppeteer/blob/v0.13.0/docs/api.md#pagepdfoptions
- # Explicitly override defaults so that we don't have unexected results
- # after puppeteer upgrades
+ # Explicitly override defaults so that we don't have unexpected results
after puppeteer upgrades.
+ # Those are default options which can be overridden by
document_options.supported_formats.{SELECTED_FORMAT}
pdf_options:
scale: 1
displayHeaderFooter: false
printBackground: false
landscape: false
pageRanges: ''
- format: 'Letter'
margin:
top: '0.5in'
right: '0.5in'
diff --git a/lib/pdf-format.js b/lib/pdf-format.js
new file mode 100644
index 0000000..9b587cd
--- /dev/null
+++ b/lib/pdf-format.js
@@ -0,0 +1,31 @@
+'use strict';
+
+module.exports = class PDFFormat {
+
+ constructor( config, format, style ) {
+ this._config = config;
+ this._format = format.toLowerCase();
+ this._style = style ? style.toLowerCase() : false;
+ }
+
+ isValid() {
+ const validFormatDefinition =
this._config.supported_formats.hasOwnProperty(this._format);
+ const validStylesDefinition = this._style ?
this._config.supported_styles.hasOwnProperty(this._style) : true;
+ return validFormatDefinition && validStylesDefinition;
+ }
+
+ /**
+ * @return {Object} A set of PDF options
+ */
+ getPDFOptions() {
+ return this._config.supported_formats[this._format];
+ }
+
+ /**
+ * @return {String[]} An array of CSS files
+ */
+ getStyles() {
+ return this._style ?
this._config.supported_styles[this._style].toString() : false;
+ }
+
+};
diff --git a/lib/queue.js b/lib/queue.js
index 5fb4624..164ec14 100644
--- a/lib/queue.js
+++ b/lib/queue.js
@@ -207,9 +207,8 @@
.articleToPdf(
data.uri,
this._puppeteerOptions,
- Object.assign(
- {}, this._pdfOptions, { format: data.format }
- ))
+ this._pdfOptions
+ )
.then((pdf) => {
renderTime = Date.now() - data._timeAtRenderStart;
this._logger.log(
diff --git a/lib/renderer.js b/lib/renderer.js
index 1809e78..564baf6 100644
--- a/lib/renderer.js
+++ b/lib/renderer.js
@@ -1,6 +1,7 @@
'use strict';
const puppeteer = require('puppeteer');
+const BBPromise = require("bluebird");
/**
* PDF renderer from a URL.
@@ -8,9 +9,10 @@
* request should create a new instance of the class.
*/
module.exports = class Renderer {
- constructor() {
+ constructor( pdfFormat ) {
this._browser = null;
this._renderAborted = false;
+ this._pdfFormat = pdfFormat;
}
/**
@@ -37,6 +39,9 @@
articleToPdf(url, puppeteerOptions, pdfOptions) {
let page;
const that = this;
+ const puppeteerPDFOptions = Object.assign(
+ {}, pdfOptions, this._pdfFormat.getPDFOptions()
+ );
return puppeteer.launch(puppeteerOptions)
.then((browser) => {
@@ -48,9 +53,18 @@
return page.goto(url, { waitUntil: 'networkidle2' });
})
.then(() => {
- return page.pdf(pdfOptions);
+ const customStyles = that._pdfFormat.getStyles();
+ if (customStyles) {
+ return page.addStyleTag({path: customStyles});
+ } else {
+ return BBPromise.resolve(page);
+ }
+ })
+ .then(() => {
+ return page.pdf(puppeteerPDFOptions);
})
.catch((error) => {
+ console.log(error);
// Only thrown an error if we didn't close the browser
ourselves
if (!this._renderAborted) {
that._closeBrowser();
diff --git a/magic.css b/magic.css
new file mode 100644
index 0000000..6cd36ff
--- /dev/null
+++ b/magic.css
@@ -0,0 +1,4 @@
+body.mw-body-content {
+ color: red;
+ font-size: 16px;
+}
diff --git a/routes/html2pdf-v1.js b/routes/html2pdf-v1.js
index 4c5b977..e30dfa0 100644
--- a/routes/html2pdf-v1.js
+++ b/routes/html2pdf-v1.js
@@ -4,6 +4,7 @@
const sUtil = require('../lib/util');
const uuid = require('cassandra-uuid');
const Renderer = require('../lib/renderer');
+const PDFFormat = require('../lib/pdf-format');
/**
* The main router object
@@ -18,7 +19,7 @@
/**
* Returns PDF representation of the article
*/
-router.get('/:title/:format(letter|a4)', (req, res) => {
+router.get('/:title/:format/:style?', (req, res) => {
const restbaseRequest = app.restbase_tpl.expand({
request: {
params: {
@@ -28,13 +29,17 @@
}
});
+ const format = new PDFFormat(app.conf.document_options, req.params.format,
req.params.style);
+ if (!format.isValid()) {
+ res.status(400).send('Unknown format');
+ return;
+ }
const id = `${uuid.TimeUuid.now().toString()}|${restbaseRequest.uri}`;
- const renderer = new Renderer();
+ const renderer = new Renderer(format);
const data = {
id,
renderer,
- uri: restbaseRequest.uri,
- format: req.params.format
+ uri: restbaseRequest.uri
};
app.queue.push(data, ((error, pdf) => {
if (error) {
--
To view, visit https://gerrit.wikimedia.org/r/405062
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I88720beba4fc13b8c65daeca5347c851b0c53cc4
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/chromium-render
Gerrit-Branch: master
Gerrit-Owner: Pmiazga <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits