From 994d3d2b896e67130d3a9d86fde523eacbb6b7c7 Mon Sep 17 00:00:00 2001 From: Charchit Date: Wed, 4 Aug 2021 01:19:38 +1000 Subject: [PATCH] Refactor to enable any key name in the request and update readMe file --- README.md | 14 ++++++++++++-- index.js | 6 +++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 516c877..fafef02 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,9 @@ Run `npm install` Run `node index.js` ## API -Papyrus exposes just one `POST` endpoint for pdf generation: `/pdf`. The webpage to be printed is provided as the -request body param `webURL`. +Papyrus exposes some `POST` endpoints for pdf generation: + +1. `/pdf`: The webpage to be printed is provided as the request body param `webURL`. Sample request: @@ -27,6 +28,15 @@ curl --location --request POST 'localhost:3000/pdf' --header 'Content-Type: appl ``` The pdf is written to the file `download.pdf`. +2. `/html2pdf`: The webpage to be printed is provided as an HTML file with any key. + +Sample request: + +```shell +curl --location --request POST 'localhost:3000/html2pdf' \ +--form 'file=@"/path/to/file/Or/Use/sample.html/from/this/repo"' > download.pdf +``` + ## Disclaimer The project could use a lot of improvements, some of which are mentioned in the issues. Feel free to add feature diff --git a/index.js b/index.js index d7909b2..56716f0 100644 --- a/index.js +++ b/index.js @@ -28,11 +28,11 @@ app.post('/pdf', (req, res) => { }); }); -app.post('/html2pdf', multer().single('file'), (req, res) => { +app.post('/html2pdf', multer().any(), (req, res) => { console.log("Generating pdf..."); - fs.writeFileSync("temp.html", req.file.buffer, 'utf8'); - var content = fs.readFileSync("temp.html"); + fs.writeFileSync("temp.html", req.files[0].buffer, 'utf8'); + var content = fs.readFileSync("temp.html", "utf-8"); pdfGenerator.generateFromHTML(content, 'webpage.pdf') .then(result => { console.log("Success: " + result);