Skip to content

Commit b5acbe8

Browse files
c7
1 parent bbe6328 commit b5acbe8

8 files changed

Lines changed: 142 additions & 0 deletions

File tree

nodejs-pdf-example-tutorial/app.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
2+
/**
3+
* Module dependencies.
4+
*/
5+
6+
var express = require('express');
7+
var routes = require('./routes');
8+
var http = require('http');
9+
var path = require('path');
10+
var fs=require('fs');
11+
var PDFDocument = require('pdfkit');
12+
13+
var app = express();
14+
15+
// all environments
16+
app.set('port', process.env.PORT || 3000);
17+
app.set('views', path.join(__dirname, 'views'));
18+
app.set('view engine', 'jade');
19+
app.use(express.favicon());
20+
app.use(express.logger('dev'));
21+
app.use(express.json());
22+
app.use(express.urlencoded());
23+
app.use(express.methodOverride());
24+
app.use(app.router);
25+
app.use(express.static(path.join(__dirname, 'public')));
26+
27+
// development only
28+
if ('development' == app.get('env')) {
29+
app.use(express.errorHandler());
30+
}
31+
32+
app.get('/', function(req,res){
33+
doc = new PDFDocument;
34+
doc.fontSize(15);
35+
//Use "\n" for breaking
36+
// Add Randam text to be added in our pdf
37+
lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam in suscipit purus.\n';
38+
lorem +='Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vivamus nec hendrerit felis. Morbi aliquam facilisis risus eu lacinia. Sed eu leo in turpis fringilla hendrerit.\n ';
39+
lorem +=' \n Ut nec accumsan nisl. Suspendisse rhoncus nisl posuere tortor tempus et dapibus elit porta. Cras leo neque, elementum a rhoncus ut, vestibulum non nibh.Phasellus pretium justo turpis. Etiam vulputate, odio vitae tincidunt ultricies, eros odio dapibus nisi, ut tincidunt lacus arcu eu elit. Aenean velit erat, vehicula eget lacinia ut, dignissim non tellus. Aliquam nec lacus mi, sed vestibulum nunc. Suspendisse potenti. Curabitur vitae sem turpis. Vestibulum sed neque eget dolor dapibus porttitor at sit amet sem. Fusce a turpis lorem. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae;';
40+
doc.text(lorem);
41+
//Add a new page for benzier curve and is part of second page
42+
doc.addPage();
43+
doc.moveTo(100, 200)
44+
.lineTo(100, 160)
45+
.quadraticCurveTo(130, 200, 150, 120)
46+
.bezierCurveTo(190, -40, 200, 200, 300, 150)
47+
.lineTo(400, 90)
48+
.stroke()
49+
//This creates our third page
50+
doc.addPage();
51+
52+
grad = doc.moveTo(200, 200).linearGradient(50, 0, 150, 100);
53+
grad.stop(0, 'green').stop(1, 'red');
54+
doc.rect(50, 0, 100, 100);
55+
doc.fill(grad);
56+
57+
grad = doc.radialGradient(300, 50, 0, 300, 50, 50);
58+
grad.stop(0, 'orange', 0).stop(1, 'orange', 1)
59+
doc.circle(300, 50, 50);
60+
doc.fill(grad);
61+
//This creates our Fourth page
62+
doc.addPage();
63+
doc.save()
64+
.moveTo(100, 150)
65+
.lineTo(100, 250)
66+
.lineTo(200, 250)
67+
.fill("#FF3300")
68+
doc.scale(0.6)
69+
.translate(470, -380)
70+
.path('M 250,75 L 323,301 131,161 369,161 177,301 z')
71+
.fill('red', 'even-odd')
72+
.restore()
73+
//This creates our Fifth page
74+
doc.addPage()
75+
.fillColor("blue")
76+
.text('Here is a link!', 100, 100)
77+
.link(100, 100, 160, 27, 'http://google.com/');
78+
doc.write("output.pdf");
79+
res.writeHead(200, {"Content-Type": "text/html"});
80+
res.write('<h2>Pdf is generated blease use below link to download<br/><br/></h2>');
81+
res.end("<a href='http://localhost:3000/download'>Download</a>");
82+
83+
});
84+
app.get('/download', function(req, res){
85+
//Use the below to send file and promt user to save it to disk
86+
res.download('output.pdf');
87+
//Use the below to send file and browser will open and display the file
88+
//res.sendfile('output.pdf');
89+
});
90+
91+
http.createServer(app).listen(app.get('port'), function(){
92+
console.log('Express server listening on port ' + app.get('port'));
93+
});
5.6 KB
Binary file not shown.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "application-name",
3+
"version": "0.0.1",
4+
"private": true,
5+
"scripts": {
6+
"start": "node app.js"
7+
},
8+
"dependencies": {
9+
"express": "3.4.4",
10+
"jade": "*",
11+
"pdfkit": "*"
12+
}
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
body {
2+
padding: 50px;
3+
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
4+
}
5+
6+
a {
7+
color: #00B7FF;
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
/*
3+
* GET home page.
4+
*/
5+
6+
exports.index = function(req, res){
7+
res.render('index', { title: 'Express' });
8+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
/*
3+
* GET users listing.
4+
*/
5+
6+
exports.list = function(req, res){
7+
res.send("respond with a resource");
8+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extends layout
2+
3+
block content
4+
h1= title
5+
p Welcome to #{title}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
doctype 5
2+
html
3+
head
4+
title= title
5+
link(rel='stylesheet', href='/stylesheets/style.css')
6+
body
7+
block content

0 commit comments

Comments
 (0)