-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use with node.js #55
Comments
this being a web-component based solution, there isn't any good tooling AFAIK that can render web-components on server That being said keep an eye on this space,
|
If you need this now, it's possible to do with jsdom. Load up the index.html in there, and mock I'm afraid I don't have a code example but this is what I've been using for the past couple of days. It's just a pain to get the blob output to a file afterwards, but things like this should work so long as you're getting globals from the jsdom window. |
For what it's worth, we were able to use RapiPdf in Node.js with something like this: const fs = require('fs');
const jsdom = require('jsdom');
const virtualConsole = new jsdom.VirtualConsole();
const {JSDOM} = jsdom;
const script = fs.readFileSync('./node_modules/rapipdf/dist/rapipdf-min.js', 'utf-8');
const json = fs.readFileSync('api.json', 'utf-8');
const obj = JSON.parse(json);
const dom = new JSDOM(`
<!doctype html>
<script>${script}</script>
<rapi-pdf id="thedoc"></rapi-pdf>
`, {
virtualConsole,
runScripts: 'dangerously'
});
const {document} = dom.window;
dom.window.onopen = function () {};
dom.window.URL.createObjectURL = (blob) => {
const reader = new dom.window.FileReader();
reader.addEventListener('loadend', () => {
fs.writeFileSync('out.pdf', Buffer.from(reader.result));
});
reader.readAsArrayBuffer(blob);
};
document.addEventListener('DOMContentLoaded', async () => {
const rapiPdf = document.querySelector('rapi-pdf');
rapiPdf.generatePdf(obj);
}); Unfortunately, it stopped working with Node.js 18. Further inspection revealed that the custom element type
There might be a way to fix this particular problem, but I wouldn't know where to look at the moment. Update: See jsdom/jsdom#3546 |
Enet4, Thanks for the example above. I used it and enhanced it. Here's a full node.js pdf document generator which I used on a WSL Ubuntu linux. /eslint-disable no-console/ const fs = require("fs"); // REPLACEREFS // replaceRefs(json) is the main function that takes the JSON object and replaces all $ref references with their corresponding content.
} function recurse(obj, parent, key) { recurse(json, null, null); // Main function to handle input and output filenames if (!inputFile) { if (!outputFile) { try {
} catch (error) { // Execute the main function |
Hello,
Is there an example on how we could use this in node js? It would be a great functionality for microservice and serverless applications.
The text was updated successfully, but these errors were encountered: