Created
January 27, 2023 06:21
-
-
Save Rigel772/1ccd21500cc693efc9f6b75003549bab to your computer and use it in GitHub Desktop.
nhost pdf2html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // import fs from "fs"; | |
| import PDFParser from "pdf2json"; | |
| const handler = async (req, res) => { | |
| res.setHeader("Access-Control-Allow-Credentials", "true"); | |
| res.setHeader("Access-Control-Allow-Origin", "*"); | |
| res.setHeader("Access-Control-Allow-Headers", "*"); | |
| res.setHeader("Access-Control-Allow-Methods", "*"); | |
| // enable CORS for OPTIONS (no need to return any body) | |
| if (req.method === "OPTIONS") { | |
| console.log("request is OPTIONS, allow all"); | |
| return res.status(204).send(); | |
| } | |
| console.log(`request body ${req.body}`); | |
| if ( | |
| req.headers["nhost-webhook-secret"] !== | |
| "5.....Z" | |
| ) { | |
| return res.status(400).send("Incorrect webhook secret"); | |
| } else { | |
| try { | |
| const pdfParser = new PDFParser(this, 1); | |
| const url = req.body.url; | |
| // FOR TESTING | |
| // const url = | |
| // "https://rejestrymedyczne.ezdrowie.gov.pl/api/rpl/medicinal-products/10965/leaflet"; | |
| // const pdfParser = new PDFParser(this, 1); | |
| const response = await fetch(url); | |
| const buffer = await response.buffer(); | |
| // SAVE PDF TO DRIVE | |
| // fs.writeFile("./url3.pdf", buffer, () => { | |
| // pdfParser.loadPDF("./url3.pdf"); | |
| // console.log(" pdf - Done."); | |
| // }); | |
| pdfParser.parseBuffer(buffer); | |
| pdfParser.on("pdfParser_dataError", (errData) => | |
| console.error(errData.parserError) | |
| ); | |
| pdfParser.on("pdfParser_dataReady", (pdfData) => { | |
| let template = pdfParser.getRawTextContent(); | |
| // The following will wrap all parts that are separated by more than one | |
| // newline in paragraphs (<p>...</p>) and insert breaks (<br>) where there | |
| // is just one newline. A text block without any newlines will simply be | |
| // wrapped in a paragraph. | |
| template = | |
| "<p>" + | |
| template | |
| .replace(/</g, "mniej niż ") | |
| .replace(/>/g, "wiecej niż ") | |
| .replace(/\n{2,}/g, "</p><p>") | |
| .replace(/\n/g, "<br />") + | |
| "</p>"; | |
| // fs.writeFile("./test4.html", template, () => { | |
| // console.log("html - Done."); | |
| // }); | |
| return res.status(200).send({ html: template }); | |
| }); | |
| } catch (error) { | |
| return res.status(500).send({ error: error.message }); | |
| } | |
| } | |
| }; | |
| export default handler; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment