Giter VIP home page Giter VIP logo

jsreport-electron-pdf's Introduction

jsreport-electron-pdf

NPM VersionLicenseBuild Status

jsreport recipe which renders pdf from html using electron

demo

Adds recipe electron-pdf to jsreport, which render pdf from html using electron.

electron-pdf recipe is capable of rendering any html and javascript you provide. This means you can also use external javascript libraries or canvas to print visual charts.

Installation

npm install jsreport-electron-pdf --save
npm install electron --save

Linux servers requires to run jsreport with this recipe through xvfb. You can find an example how to do it here. Note that electron-pdf is also included in the jsreport full docker image which can simplify the installation on linux targets.

jsreport-core

If you use jsreport-core, you can apply this extension manually and pass configuration to it directly:

var jsreport = require('jsreport-core')();
jsreport.use(require('jsreport-electron-pdf')({ strategy: 'electron-ipc' }));

Configuration

Use electron-pdf key in the standard jsreport config file.

Available options:

"extensions": {
  "electron-pdf": {
    /* use rather dedicated process for every generation,
      dedicated-process strategy is quite slower but can solve some bugs
      with corporate proxy, set to 'electron-server' for maximum performance.

      possible values: 'dedicated-process', 'electron-ipc' or 'electron-server', defaults to 'dedicated-process' */
    "strategy": "electron-ipc",
    /* number of allocated electron processes (when using electron-server strategy) */
    "numberOfWorkers": 2,
    /* time in ms to wait for worker ping response in order to be considered alive when using `electron-server` or `electron-ipc` strategy */
    "pingTimeout": 100,
    /* timeout in ms for pdf generation, when the timeout is reached, the conversion is cancelled */
    "timeout": 5000,
    /* optional port range where to start electron server (when using electron-server strategy) */
    "portLeftBoundary": 1000,
    "portRightBoundary": 2000,
    /* optional hostname where to start electron server when using electron-server strategy) */
    "host": '127.0.0.1',
    /* optional chrome command line switches for electron process, see https://electron.atom.io/docs/api/chrome-command-line-switches/ for details. */
    "chromeComandLineSwitches": {
      'disable-http-cache': null
    },
    /* set to true to allow requests using the file protocol (file:///). defaults to false */
    "allowLocalFilesAccess": false,
    /* the collected console.log, console.error, console.warn messages are trimmed by default */
    "maxLogEntrySize": 1000
  }
}

Settings

electron-pdf recipe uses electron's printToPDF feature to generate PDF from a web page, so basically most options are the same as electron's printoToPDF options.

Available settings for pdf generation:

  • marginsType Number - specify the type of margins to use in PDF.
    • 0 - default
    • 1 - none
    • 2 - minimum
  • landscape Boolean - true for landscape, false for portrait.
  • format String - predefined page sizes or Object - { width, height } in microns.
    • A4
    • A3
    • Legal
    • Letter
    • Tabloid
    • Object|JSON Object String. Keys: width, height. Values: integer in microns (micrometers) (1/1000mm)
      • e.g. A5: "{ 'width': 148000, 'height': 210000 }"
  • printBackground Boolean - whether to print CSS backgrounds or not.
  • width Number - width (px) of the web page (BrowserWindow) that would be used to generate the PDF.
  • height Number - height (px) of the web page (BrowserWindow) that would be used to generate the PDF.
  • printDelay Number - delay between rendering a page and printing into pdf, this is useful when printing animated content like charts, or you can use waitForJS option.
  • blockJavaScript Boolean - whether to disable javascript execution in page or not.
  • waitForJS Boolean - if it is set to true the PDF generation will wait until you set window.JSREPORT_READY_TO_START to true in your page.

Page breaks

Css contains styles like page-break-before you can use to specify html page breaks. This can be used as well with electron-pdf to specify page breaks inside pdf files.

<h1>Hello from Page 1</h1>

<div style='page-break-before: always;'></div>

<h1>Hello from Page 2</h1>

<div style="page-break-before: always;"></div>

<h1>Hello from Page 3</h1>

Headers and footers

Attach a header and footer to PDF is not currently supported :(, unlike phantomjs, electron does not provide a way to attach a header/footer to the final PDF.

Printing triggers

You may need to postpone pdf printing until some javascript async tasks are processed. If this is your case set the waitForJS: true option in the API or Wait for printing trigger in the studio menu. Then the printing won't start until you set window.JSREPORT_READY_TO_START=true inside your template's javascript.

...
<script>
    // do some calculations or something async
    setTimeout(function() {
        window.JSREPORT_READY_TO_START = true; // this will start the pdf printing
    }, 500);
    ...
</script>

Twitter Bootstrap

Using a responsive css framework for printing pdf may not be the best idea. However it still works. Only thing you need to keep in mind is that output pdf typically won't look the same as html because bootstrap includes different printing styles under @media print.

Troubleshooting

Using electron in single core machines

If you are using a machine with a single-core processor you will probably experience a high CPU usage when doing any rendering (97% in most cases and the usage is worse when using Windows), this is because a limitation in electron when it is being used on single core machines, unfortunately the only way to overcome this is to upgrade your machine to a processor with more cores (a processor with two cores is fine). more info: issue1, issue2

See electron-html-to for more troubleshooting

License

See license

jsreport-electron-pdf's People

Contributors

angaz avatar bjrmatos avatar pofider avatar spence-s avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jsreport-electron-pdf's Issues

Electron-pdf issue in JSReport

Hi,
I am runnning jsreport in windows 10 and installed electron-pdf plugins in this way:
npm install jsreport-electron-pdf --save
npm install electron --save

I have attached jsreport.config.json and package,json configuration files

jsreport.config.json.txt
package.json.txt

I am trying to generate pdf using electron-pdf engine using the reports api:
http://localhost:5488/reports/api
{
"template": {
"content" : "Hello world {{name}}",
"recipe": "electron-pdf",
"engine": "handlebars",
"chrome": {
"landscape": true
}
},
"data" : { "name": "JSreports PDF"},
}

Getting this error during the processing of this request
2020-04-24T07:18:46.203Z - error: Error when processing render request 1 Worker Timeout, the worker process does not respond after 359127 ms Error: Worker Timeout, the worker process does not respond after 359127 ms
at Timeout._onTimeout (C:\jsreports\jsreportapp\node_modules\electron-workers\lib\ElectronManager.js:377:21)
at listOnTimeout (internal/timers.js:549:17)
at processTimers (internal/timers.js:492:7)

Please help me with this issue

Anchor Tags

CNN

On the PDF I get both CNN with a link (Expected) but also the URL itself (http://www.cnn.com)

Am I missing something? Is it possible to remove? The real goal is a thumbnail image that links to a larger image.

Installation question

Probably very dumb question. Why is not the electron-prebuilt in the package.json dependencies so the user don't have to install it additionally?

electron-server crashing

While rendering a large PDF, using "electron-server" strategy jsreport crashed with error.
When I switched to "electron-ipc" or "dedicated-process", the PDF is rendered in about 4 minutes, with 2,498 pages...

I am wanting to use "electron-server" as documentation says that it has most performance....

here is the error when it crashed using "electron-server"

2016-11-16T20:53:47.638Z - error: Error when processing render request Error during rendering report: socket hang up Error: socket hang up
    at createHangUpError (_http_client.js:252:15)
    at Socket.socketOnEnd (_http_client.js:344:23)
    at emitNone (events.js:91:20)
    at Socket.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)
From previous event:
    at X:\Projects\jsreport\node_modules\jsreport-core\lib\render\render.js:144:54
    at _fulfilled (X:\Projects\jsreport\node_modules\listener-collection\node_modules\q\q.js:794:54)
    at self.promiseDispatch.done (X:\Projects\jsreport\node_modules\listener-collection\node_modules\q\q.js:823:30)
    at Promise.promise.promiseDispatch (X:\Projects\jsreport\node_modules\listener-collection\node_modules\q\q.js:756:13)
    at X:\Projects\jsreport\node_modules\listener-collection\node_modules\q\q.js:564:44
    at flush (X:\Projects\jsreport\node_modules\listener-collection\node_modules\q\q.js:110:17)
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9) requestId=7
2016-11-16T20:53:47.646Z - error: Error during processing request: http://IP_ADDRESS/api/report details: Error during rendering report: socket hang up Error: socket hang up
    at createHangUpError (_http_client.js:252:15)
    at Socket.socketOnEnd (_http_client.js:344:23)
    at emitNone (events.js:91:20)
    at Socket.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)
From previous event:
    at X:\Projects\jsreport\node_modules\jsreport-core\lib\render\render.js:144:54
    at _fulfilled (X:\Projects\jsreport\node_modules\listener-collection\node_modules\q\q.js:794:54)
    at self.promiseDispatch.done (X:\Projects\jsreport\node_modules\listener-collection\node_modules\q\q.js:823:30)
    at Promise.promise.promiseDispatch (X:\Projects\jsreport\node_modules\listener-collection\node_modules\q\q.js:756:13)
    at X:\Projects\jsreport\node_modules\listener-collection\node_modules\q\q.js:564:44
    at flush (X:\Projects\jsreport\node_modules\listener-collection\node_modules\q\q.js:110:17)
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)
    

Slow.....

I have a report which uses plain HTML table and handlebars.
Report has 8000+ rows. No scripts of any kind - just pure HTML to PDF.

With phantomjs it takes about 15 seconds to render.
I tried using electron recipe with 'electron-ipc' and 'electron-server' strategy and
the same report takes 40+ seconds to render....

Any idea why would it be that much slower?

Electron-PDF timing out and looks like configuration isn't picking up from jsreport.config.json

Issue

  • I'm trying to generate a report via electron-pdf and it's timing out with the following error:
Error: Timeout when executing in electron
    at Timeout._onTimeout (/home/ubuntu/jsreportapp/node_modules/electron-html-to/lib/dedicatedProcessStrategy.js:277:20)
    at ontimeout (timers.js:498:11)
    at tryOnTimeout (timers.js:323:5)
    at Timer.listOnTimeout (timers.js:290:5)
  • Would very much appreciate any assistance ๐Ÿ™

Steps

  • I installed jsreport-electron-pdf and electron since electron-prebuilt is now Deprecated.
  • Here's the extract from my package.json:
"dependencies": {
    "electron": "^2.0.5",
    "install": "^0.12.1",
    "jsreport": "2.1.1",
    "jsreport-electron-pdf": "^3.0.0",
    "jsreport-phantom-pdf": "^2.1.3",
    "npm": "^6.2.0"
  }
  • and here's the section from my jsreport.config.json file:
"extensions": {
    "authentication": {
      "cookieSession": {
        "secret": "some secret"
      },
      "admin": {
        "username": "some name",
        "password": "some password"
      },
      "enabled": true
    },
    "scripts": {
      "timeout": 40000,
      "strategy": "http-server"
    },
    "electron":{
      "strategy":"electron-ipc",
      "pingTimeout":100,
      "timeout":5000
    }
  },

Description

  • I'm running on an AWS EC2 t2.medium instance so that has 2 cores
  • However the electron-pdf strategy is defaulting to dedicated-process even though I'm setting it as electron-ipc in the jsreport "extensions" configuration.
  • It's not even taking the pingTimeout and timeout values from the config file
  • startup log
2018-07-15T10:59:39.982Z - info: Using extension electron-pdf
  electron-html-to Creating a new conversion function with options: +0ms { timeout: 10000,
  numberOfWorkers: 2,
  chromeCommandLineSwitches: { 'ignore-certificate-errors': null },
  allowLocalFilesAccess: false,
  maxLogEntrySize: 1000,
  tmpDir: '/tmp/jsreport/autocleanup',
  strategy: 'dedicated-process' }

  • Jsreport Server Log
2018-07-15T11:00:11.394Z - debug: Electron Pdf recipe start.
  electron-html-to:conversion generating new conversion task.. +31s
  electron-html-to:conversion conversion task id: ea778c01-9b8d-4672-865a-ee5c18cb5328 +0ms
  electron-html-to:conversion creating temporal html files in /tmp/jsreport/autocleanup.. +1ms
  electron-html-to:conversion creating temporal html file [type: html] in /tmp/jsreport/autocleanup/ea778c01-9b8d-4672-865a-ee5c18cb5328html.html.. +0ms
  electron-html-to:conversion starting conversion task [strategy:dedicated-process][task id:ea778c01-9b8d-4672-865a-ee5c18cb5328] with options: +1ms { browserWindow: 
   { width: undefined,
     height: undefined,
     webPreferences: { javascript: true } },
  waitForJSVarName: 'JSREPORT_READY_TO_START',
  collectLogs: true,
  delay: undefined,
  waitForJS: false,
  converterPath: '/home/ubuntu/jsreportapp/node_modules/electron-html-to/lib/converters/pdf.js',
  pdf: 
   { marginsType: undefined,
     pageSize: undefined,
     printBackground: true,
     landscape: false },
  htmlFile: '/tmp/jsreport/autocleanup/ea778c01-9b8d-4672-865a-ee5c18cb5328html.html',
  url: 'file:///tmp/jsreport/autocleanup/ea778c01-9b8d-4672-865a-ee5c18cb5328html.html',
  chromeCommandLineSwitches: { 'ignore-certificate-errors': null },
  extraHeaders: {},
  output: 
   { tmpDir: '/tmp/jsreport/autocleanup',
     id: 'ea778c01-9b8d-4672-865a-ee5c18cb5328' } }
  electron-html-to:dedicated-process-strategy saving settings in temporal file.. +1ms
  electron-html-to:dedicated-process-strategy searching electron executable path.. +0ms
  electron-html-to:dedicated-process-strategy trying to get electron path from "electron" module.. +0ms
  electron-html-to:dedicated-process-strategy spawning new electron process with args: +1ms [ '/home/ubuntu/jsreportapp/node_modules/electron-html-to/lib/scripts/standaloneScript.js' ] and options: { env: 
   { ELECTRON_WORKER_ID: 'ea778c01-9b8d-4672-865a-ee5c18cb5328',
     ELECTRON_HTML_TO_SETTINGS_FILE_PATH: '/tmp/jsreport/autocleanup/ea778c01-9b8d-4672-865a-ee5c18cb5328settings.html',
     DISPLAY: 'localhost:10.0' },
  stdio: [ null, null, null, 'ipc' ] }
  electron-html-to:dedicated-process-strategy electron process pid: +4ms 2196
  electron-html-to:dedicated-process-strategy processing conversion.. +1ms
  electron-html-to:dedicated-process-strategy electron process exit with code: 127 and signal: null +1ms
  electron-html-to:dedicated-process-strategy conversion timeout.. +10s
2018-07-15T11:00:21.409Z - error: Error when processing render request Timeout when executing in electron Error: Timeout when executing in electron
    at Timeout._onTimeout (/home/ubuntu/jsreportapp/node_modules/electron-html-to/lib/dedicatedProcessStrategy.js:277:20)
    at ontimeout (timers.js:498:11)
    at tryOnTimeout (timers.js:323:5)
    at Timer.listOnTimeout (timers.js:290:5)
2018-07-15T11:00:21.410Z - error: Error during processing request at http:/<IP_Address>:5488/api/report/student-list-normal-marks

Debug logs from console

jsreport includes debug extension which can collect logs associated with particular rendering request and response them back to the caller. This is for example used in combination with phantom-pdf recipe which forwards all the phantomjs messages and also user's console.log calls into this log which is usually displayed in the jsreport studio.

It would be nice to have the similar functionality in this recipe. Would it be possible?

blog post introducing debug extension
http://jsreport.net/blog/debug-extension

writing to request logs
https://github.com/jsreport/jsreport-phantom-pdf/blob/master/lib/phantom.js#L159

overriding console
https://github.com/pofider/phantom-html-to-pdf/blob/master/lib/scripts/logScriptPart.js

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.