Created
January 30, 2025 19:07
-
-
Save archiewood/ec67d2f693a5d0b39fd642c1719005a9 to your computer and use it in GitHub Desktop.
Login and generate a PDF from an Evidence app
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 puppeteer from 'puppeteer'; | |
| import * as dotenv from 'dotenv'; | |
| // Load environment variables | |
| dotenv.config(); | |
| const generatePDF = async () => { | |
| try { | |
| // Load URL, username, and password from environment variables | |
| const url = process.env.PDF_URL || 'https://example.com'; | |
| const loginUrl = process.env.LOGIN_URL || 'https://example.com/login'; | |
| const username = process.env.USERNAME || 'your_username'; | |
| const password = process.env.PASSWORD || 'your_password'; | |
| const outputPath = 'report.pdf'; // Path for the generated PDF | |
| console.log(`Generating PDF from: ${url}`); | |
| // Launch Puppeteer | |
| const browser = await puppeteer.launch(); | |
| const page = await browser.newPage(); | |
| // Navigate to the login page | |
| console.log(`Navigating to login page: ${loginUrl}`); | |
| await page.goto(loginUrl, { waitUntil: 'networkidle2' }); | |
| // Enter username and password | |
| console.log('Entering login credentials...'); | |
| await page.type('input[id="email"]', username); // Adjust selector as needed | |
| await page.type('input[type="password"]', password); // Adjust selector as needed | |
| console.log('Clicking the sign-in button...'); | |
| const signInButton = await page.evaluateHandle(() => { | |
| // Find the button based on its attributes and content | |
| const buttons = Array.from(document.querySelectorAll('button')); | |
| return buttons.find(button => button.textContent.trim() === 'Sign in' && !button.disabled); | |
| }); | |
| if (signInButton) { | |
| await signInButton.click(); | |
| } else { | |
| throw new Error('Sign-in button not found'); | |
| } | |
| // Wait for navigation after login | |
| await page.waitForNavigation({ waitUntil: 'networkidle2' }); | |
| console.log('Logged in successfully.'); | |
| // Navigate to the target URL | |
| console.log(`Navigating to target URL: ${url}`); | |
| await page.goto(url, { waitUntil: 'networkidle2' }); | |
| // Wait for 5 seconds for the page to load | |
| await new Promise(resolve => setTimeout(resolve, 5000)); | |
| // Generate the PDF | |
| await page.pdf({ | |
| path: outputPath, | |
| format: 'Letter', | |
| margin: { | |
| top: '1in', | |
| right: '1in', | |
| bottom: '1in', | |
| left: '1in', | |
| }, | |
| }); | |
| console.log(`PDF generated successfully: ${outputPath}`); | |
| // Close the browser | |
| await browser.close(); | |
| } catch (error) { | |
| console.error('Error generating PDF:', error); | |
| process.exit(1); | |
| } | |
| }; | |
| generatePDF(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment