Created
January 19, 2024 08:30
-
-
Save saurabh2590/97d963cfea25aa5d70daf278451bde13 to your computer and use it in GitHub Desktop.
GeneratePDF for webpage
This file contains 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 * as puppeteer from 'puppeteer'; | |
import * as fs from 'fs'; | |
import * as readline from 'readline'; | |
async function downloadWebpageAsPdf(url: string, outputFilePath: string): Promise<void> { | |
const browser = await puppeteer.launch(); | |
const page = await browser.newPage(); | |
try { | |
await page.goto(url, { waitUntil: 'networkidle2' }); | |
await page.pdf({ path: outputFilePath, format: 'A4' }); | |
console.log(`PDF saved to: ${outputFilePath}`); | |
} catch (error) { | |
console.error('Error generating PDF:', error); | |
} finally { | |
await browser.close(); | |
} | |
} | |
async function processLineByLine() { | |
const fileStream = fs.createReadStream('./ticket-ids.csv'); | |
const rl = readline.createInterface({ | |
input: fileStream, | |
crlfDelay: Infinity, | |
}); | |
// Note: we use the crlfDelay option to recognize all instances of CR LF | |
// ('\r\n') in input.txt as a single line break. | |
for await (const line of rl) { | |
// Each line in input.txt will be successively available here as `line`. | |
if (fs.existsSync(`trac_pdf_export/TR-${line}.pdf`)) { | |
console.log(`Already download file for trac id ${line}`); | |
} else { | |
console.log(`Downloading trac id ${line}`) | |
await downloadWebpageAsPdf(`http://LDAP_USER:LDAP_PASSWORD@trac.bm.loc/ticket/${line}`, `trac_pdf_export/TR-${line}.pdf`); | |
} | |
} | |
} | |
processLineByLine().then(() => console.log("done")).catch((err) => console.error(err)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment