question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Express.js Working Example

See original GitHub issue

Thank 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:closed
  • Created 4 years ago
  • Reactions:12
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
dhollenbeckcommented, Aug 22, 2019

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.

5reactions
scottie-schneidercommented, Aug 26, 2019

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); })

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found