Skip to content

Possible to append arbitrary data to PDFs, like CSVs? #740

@mrchrisadams

Description

@mrchrisadams

Hi there,

I recently read this blog post about using PDFs as a container for bundling documents like reports with structured data like JSON, or csv files.

In the world of C#, there is now a tool that I understand will let you take a PDF file, and then append an arbitrary csv or json file, so you can bundle structured data into a PDF:

http://tomforth.co.uk/pdfattacher/

Something like this also appears to have been implemented in Java too, here

https://github.com/Aiybe/PDFData

How complex would it be using pdfkit, so you can run it with on the command line, or incorporate it into a cross platform electron app?

I can see there are methods like doc.note(), or doc.image(), but they seem to assume the presence of a page there already, and as I understand it the idea of attaching data to the PDF container works at a level up from a page.

Guessing what an API might look like:

PDFDocument = require 'pdfkit'

# Create a document
doc = new PDFDocument

# Pipe its output somewhere, like to a file or HTTP response
# See below for browser usage
doc.pipe fs.createWriteStream('output.pdf')

# Add text as per normal examples
doc.text('Here is some narrative text !', 100, 100)

# Attach json file
doc.data('path/to/data.json', datatype: "json")

# Finalize PDF file
doc.end()

I'm not that familiar with the PDF format, so if there's a link you'd suggest I look into, to understand more about how to work with pdfkit for this, I'm happy to do so.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions