electron-html-to



Highly scalable html conversion in scale
This module let you convert a web page (html, css, js) in any format you want (via a converter function) using electron.
Works in electron@>=0.36.1 including electron@1
var fs = require('fs'),
convertFactory = require('electron-html-to');
var conversion = convertFactory({
converterPath: convertFactory.converters.PDF
});
conversion({ html: '<h1>Hello World</h1>' }, function(err, result) {
if (err) {
return console.error(err);
}
console.log(result.numberOfPages);
console.log(result.logs);
result.stream.pipe(fs.createWriteStream('/path/to/anywhere.pdf'));
conversion.kill();
});
Built-in converters
convertFactory.converters.PDF
(html to pdf) -> when the conversion ends the result
param will have numberOfPages
(Number) and stream
(Stream) properties.
Custom converters
Converters are functions that run in the electron process, see the pdf conversion implementation for an example.
Global options
var conversion = require('electron-html-to')({
pathToElectron: '/path/to/custom/electron-executable',
electronArgs: ['--some-value=2', '--enable-some-behaviour'],
converterPath: '/path/to/a/converter.js'
numberOfWorkers: 2,
pingTimeout: 100,
timeout: 5000,
tmpDir: 'os/tmpdir',
portLeftBoundary: 1000,
portRightBoundary: 2000,
host: '127.0.0.1',
allowLocalFilesAccess: false,
maxLogEntrySize: 1000,
chromeCommandLineSwitches: {
'disable-http-cache': null,
'log-net-log': '/path/to/save'
},
strategy: 'electron-ipc | electron-server | dedicated-process'
});
Local options
conversion({
html: '<h1>Hello world</h1>',
url: 'http://jsreport.net',
delay: 0,
collectLogs: true,
waitForJS: true,
waitForJSVarName: 'MY_CUSTOM_VAR_NAME',
userAgent: 'CUSTOM_USER_AGENT',
extraHeaders: {
'X-Foo': 'foo',
'X-Bar': 'bar'
},
converterPath: '/path/to/a/converter.js',
browserWindow: {
width: 600,
height: 600,
x: 0,
y: 0,
useContentSize: false,
webPreferences: {
nodeIntegration: false,
partition: '',
zoomFactor: 3.0,
javascript: true,
webSecurity: false,
allowDisplayingInsecureContent: true,
allowRunningInsecureContent: true,
images: true,
java: true,
webgl: true,
webaudio: true,
plugins: ,
experimentalFeatures: ,
experimentalCanvasFeatures: ,
overlayScrollbars: ,
overlayFullscreenVideo: ,
sharedWorker: ,
directWrite:
}
},
pdf: {
marginsType: 0,
pageSize: 'A4',
printBackground: false,
landscape: false
}
}, cb);
Local resources
You can add local files like .css
, .jpg
or .js
files by setting the
allowLocalFilesAccess
option to true. This option allow requests with the file protocol file:///
.
Example:
<head>
<link rel="stylesheet" href="/css/pdf.css">
</head>
<body>
<h1 class="title">It Works!!</h1>
<img src="/images/company_logo.jpg" title="MyLogo">
</body>
If your html doesn't have url in the form of file://path/to/you/base/public/directory
you would need to transform paths from /images/company_logo.jpg
to file://path/to/you/base/public/directory/images/company_logo.jpg
.
const fs = require('fs');
const convertFactory = require('electron-html-to');
fs.readFile('index.html', 'utf8', (err, htmlString) => {
htmlString = htmlString.replace(/href="|src="/g, match => {
return match + 'file://path/to/you/base/public/directory';
});
const conversion = convertFactory({
converterPath: convertFactory.converters.PDF,
allowLocalFilesAccess: true
});
conversion({ html: htmlString }, (err, result) => {
if (err) return console.error(err);
result.stream.pipe(fs.createWriteStream('/path/to/anywhere.pdf'));
conversion.kill();
});
});
Kill workers
conversion.kill();
Programmatic conversion
If you need to programmatic trigger the conversion process (because you need to calculate some values or do something async in your page before convert it) you can enable the waitForJS
local option, when waitForJS
is set to true the conversion will wait until you set a variable to true in your page, by default the name of the variable is ELECTRON_HTML_TO_READY
but you can customize it via waitForJSVarName
option.
Example
local options:
conversion({
html: '<custom html here>',
waitForJS: true
}, cb);
custom html:
<h1></h1>
<script>
setTimeout(function() {
window.ELECTRON_HTML_TO_READY = true;
}, 500);
</script>
Debugging
-
To get more information (internal debugging logs of the module) about what's happening inside the conversion run your app with the DEBUG
env var: DEBUG=electron-html-to,electron-html-to:* node app.js
(on Windows use set DEBUG=electron-html-to,electron-html-to:* && node app.js
). This will print out some additional information about what's going on.
-
To see the electron process UI created (the visible electron window) and point stdout/stderr of the electron processes to console run your app with the ELECTRON_HTML_TO_DEBUGGING
env var: ELECTRON_HTML_TO_DEBUGGING=true node app.js
(on Windows use set ELECTRON_HTML_TO_DEBUGGING=true && node app.js
).
-
To only point stdout/stderr of the electron processes to console run your app with the ELECTRON_HTML_TO_STDSTREAMS
env var: ELECTRON_HTML_TO_STDSTREAMS=true node app.js
(on Windows use set ELECTRON_HTML_TO_STDSTREAMS=true && node app.js
).
-
To enable low level messages (chromium logs) of the electron processes run your app with the ELECTRON_ENABLE_LOGGING
env var: ELECTRON_ENABLE_LOGGING=true node app.js
(on Windows use set ELECTRON_ENABLE_LOGGING=true && node app.js
).
Requirements
- Install electron >= 0.36.1 including electron@1, the easy way to install
electron in your app is
npm install electron --save
or npm install electron-prebuilt --save
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 conversion (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
env: node: No such file or directory when using electron-prebuilt and nvm
If you are using node with nvm and you have installed electron with npm install -g electron-prebuilt
you probably will see an error or log with env: node: No such file or directory
, this is because the electron executable installed by electron-prebuilt
is a node CLI spawning the real electron executable internally, since nvm don't install/symlink node to /usr/bin/env/node
when the electron executable installed by electron-prebuilt
tries to run, it will fail because node
won't be found in that context..
Solution:
1.- Install electron-prebuilt
as a dependency in your app, this is the option recommended because you probably want to ensure your app always run with the exact version you tested it, and probably you don't want to install electron globally in your system.
2.- You can make a symlink to /usr/bin/env/node
but this is not recommended by nvm authors, because you will loose all the power that nvm brings.
3.- Put the path to the real electron executable in your $PATH
.
License
See license