Curtis Larson's Blog    Main Site    Portfolio     Archive     Feed

Meteor - Dynamically Render a PDF with iron:router

Introduction

A cool trick I found out recently is how to dynamically render a pdf from the server to the client using iron:router. The idea is you have a pdf object stored in a database or external file store and you want to transfer that pdf to the client without directly accessing the pdf file. We are going to accomplish this with a server side iron:router route. You can access the full code for this tutorial on my GitHub page here.

The Route File

The first method of rendering the pdf from the server involves the wonderful CollectionFS package which can be used to store files in a variety of different data stores including mongoDB, dropbox, and Amazon S3. We can accomplish this with the below code:

Some important thigns to note include the creation of a readable object through file.createReadStream("tmp") which we then read into the buffer object. Once we have completely read from the FS.File object we can write the response to the client through the node this.response.write(buffer); statement. Also make sure to include the where: "server" in the route to ensure that it only executes as a server side route.

An additional way to render a pdf from the server to the client is to read the pdf from the file system, read it into a buffer using the node.js fs package, and feed it to the client. Make sure you install the meteorhacks:npm package so you can include the fs node.js package. You can accomplish all this with the below code:

Pretty straight forward and similar to the above gist.

Conclusion

That’s it for this tutorial, hopefully it helps out anyone who wants to dynamically render a pdf from the server without directly accessing the file. Again the full code for this tutorial is available on my GitHub page. If you have any questions please leave them in the comments!


Curtis Larson is a LA based freelance software developer. Follow him on Twitter or send him an email if you're looking for a talented developer.