Express.js Working Example
See original GitHub issueThank you very much for this amazing lib! Great job!
I would like to use pdf-lib
in my express.js app. Below is a working example of an express.js app sending a Hello World pdf which Chrome reads just fine.
Am I doing things correctly?
'use strict';
var PdfLib = require('pdf-lib');
var PDFDocument = PdfLib.PDFDocument;
var StandardFonts = PdfLib.StandardFonts;
var rgb = PdfLib.rgb;
var express = require('express');
var app = express();
var port = 3000;
async function createPdf() {
var pdfDoc = await PDFDocument.create();
var timesRomanFont = await pdfDoc.embedFont(StandardFonts.TimesRoman);
var page = pdfDoc.addPage();
var { width, height } = page.getSize();
var fontSize = 30;
page.drawText('Hello World', {
x: 50,
y: height - 4 * fontSize,
size: fontSize,
font: timesRomanFont,
color: rgb(0, 0.53, 0.71),
});
var pdfBytes = await pdfDoc.save();
var pdfBuffer = Buffer.from(pdfBytes.buffer, 'binary');
return pdfBuffer;
}
app.get('/', function(req, res) {
createPdf().then(function(pdfBuffer) {
res.status(200);
res.type('pdf');
res.send(buffer);
}).catch(function (err) {
res.status(500);
res.send(err.message);
});
});
app.listen(port, function() {
console.log(`Example app listening on port ${port}!`);
});
Issue Analytics
- State:
- Created 4 years ago
- Reactions:12
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Express examples - Express.js
This page contains list of examples using Express. auth - Authentication with login and password; content-negotiation - HTTP content negotiation; cookie- ...
Read more >Express Explained with Examples - Installation, Routing ...
Express is the most popular Node.js framework because it requires minimum setup to start an application or an API and is fast, and...
Read more >Node.js - Express Framework - Tutorialspoint
Hello world Example ... Following is a very basic Express app which starts a server and listens on port 8081 for connection. This...
Read more >Express/Node introduction - Learn web development | MDN
Express provides methods to specify what function is called for a particular HTTP verb ( GET , POST , SET , etc.) and...
Read more >Node.js Express FrameWork Tutorial – Learn in 10 Minutes
var express=require('express'); var app=express(); app.get('/',function(req,res) { res.send('Hello World!'); }); var server=app.listen ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The example code above works correctly with no problems. I just posted it in case it helps others. @Hopding your work is awesome. Thank you so much.
Thank you for leaving this. Allowed me to finally get express up and running after 3 days of frustration. One slight modification to the code:
createPdf().then(function(pdfBuffer) { res.status(200); res.type('pdf'); res.send(pdfBuffer); })