Proton

Proton allows users to download a Wikipedia article as PDF. It supports both desktop and mobile-friendly prints.

Technical details
Proton is a simple service that generates the PDF using Chromium driven by Puppeteer library. It consists of two components:
 * Queue system that queues all requests (as PDF generation is both resource and time-intensive job)
 * Renderer code which instructs Puppeteer to print requested page as PDF.

Proton is structured as a web service and is written in Javascript, making use of Node.js. It is intended to provide beautiful and clean PDFs. On Wikimedia wikis, Proton will be proxied behind RESTBase. It uses the puppeteer-core library, chromium browser is not bundled with puppeteer-core and it has to be downloaded separately. The  environment variable is used to point to chromium executable.

The best way to generate the Article PDF is to use browser built-in to PDF functionality. That method provides the best results and additionally allows us to reuse the existing print styles available for both Desktop and Mobile versions of Wikipedia. The system doesn't post-process the requested HTML. Articles are printed the same way as they appear in print preview in the user browser. The generated PDFs are very similar (if not identical) to what anyone can achieve by using Print to PDF on their Chrome browser. To get best results, Proton disables the Javascript. It is done to disable all dynamic content transformations, like lazy-loaded images on Mobile pages.

Note: for some users, the PDF they get from browser print and the one they get from Proton service might differ a bit as fonts configuration on user system can have specific settings related to fonts hinting/kerning.

QueueSystem
The Queue system is the heart of Proton renderer. It handles the flow of each job through waiting/processing/timeout logic. Each job in the queue can have two states - waiting and processing. The queue system not only allows a specific amount of jobs to run at the same time but it also handles job timeouts and job cancellation. Because of the queue complexity, we had to implement the solution that allows us to:
 * limit the number of waiting jobs
 * after a defined amount of seconds reject the waiting job
 * limit the number of rendering jobs (as PDF rendering requires lots of resources)
 * a safety net to reject rendering jobs that takes too much time
 * to save resources, when the request is aborted queue will try to cancel the job, doesn't matter which state the aborted job is (processing/rendering)

Renderer
The Renderer is a simple facade to access  method from puppeteer library. Renderer is responsible for setting proper chromium environment and browser viewport, requesting the Wikipedia page, calling the  function. Plus it keeps an eye on the browser process. Each render starts new Chromium instance, and after successful render, the chromium process exists. To save resources, and keep our system in good state Renderer asks Chromium to shut down and if because of any reason browser still keeps processing the request it will send the  to browser process to make sure it doesn't use any more CPU nor the memory.

Additional features
When a job fails because the queue is full or job timeouts in any state the Proton service will return  response with Retry-After header. The Retry-After header instruments load balancer to depool given Proton node so it can finish processing current jobs. System sets Retry-After header to  configuration value. After that time all processing jobs should finish, and the system should be able to pick up new jobs.

GitHub Repository: https://github.com/wikimedia/mediawiki-services-chromium-render

Development
Development happens in the. Code review happens in Gerrit. See Gerrit/Getting started to set up an account for yourself. Service uses the ServiceTemplateNode project template and follows all Service development rules.

Running the tests
To run all swagger tests and mocha tests:

npm test

To run all coverage test:

npm run coverage

Technical documents

 * README.MD has the documentation about Proton internals and configuration variables.
 * ServiceTemplateNode/Deployment How to deploy Proton service
 * Reading/Web/Projects/Print_Styles Print styles and limitations are identical to Browser print styles.

Links for Proton developers

 * Puppeteer documentation

Links for Proton deployers (to the Wikimedia cluster)

 * Kibana dashboard
 * Grafana dashboard for PDF metrics
 * Prometheus breakdown for the Proton cluster on codfw
 * Prometheus breakdown for the Proton cluster on eqiad
 * Jenkins Job Builder docs for updating jenkins jobs
 * Icinga uses the swagger file and performs HTTP checks for Desktop/Mobile prints plus it verifies the requesting non-existing page.

Contact
If you need help or have questions/feedback, you can contact us in or the wikitext-l mailing list.