Skip to content

Commit

Permalink
V 1.0👀
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianArado committed Jan 16, 2021
0 parents commit 52f0a2a
Show file tree
Hide file tree
Showing 5 changed files with 1,996 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
53 changes: 53 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const express = require('express');
const puppeteer = require('puppeteer');

// Initiliaze the express app
const app = express();

// EJS
app.set('view engine', 'ejs');


app.get('/', (req, res) => res.render('index'));

app.get(`/pdf`, async (req, res) => {
try {
// cache the url query of the page that you want to print
const url = req.query.target;
// Create a browser instance with Puppeteer
const browser = await puppeteer.launch({ headless: true });
// Create a new page instance
const webPage = await browser.newPage();
// set the viewport for the page you want to crawl
await webPage.setViewport({ width: 800, height: 600 });
// wait until there are no new network connection
await webPage.goto(url, { waitUntil: 'networkidle0' });

// Create the PDF from the crawled page and save it to our device
const pdf = await webPage.pdf({
printBackground: true,
format: 'letter',
margin: {
top: '20px',
bottom: '40px',
left: '20px',
right: '20px',
},
});

// close the browser connection after PDF creation is over
await browser.close();

// send the created file
res.contentType('application/pdf');
res.send(pdf);
} catch (error) {
console.log(error.message);
}
});

// Start the server on port 3000
const PORT = process.env.PORT || 3000 ;
app.listen(PORT, () => {
console.log(`Server started on port ${PORT}`);
});
Loading

0 comments on commit 52f0a2a

Please sign in to comment.