Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node JS get the first page of PDF buffer

I am looking for a simple way to get the first page of a PDF in NodeJS. The PDF is available as a buffer (Uint8Array) and the first page should be a buffer.

like image 629
user1791139 Avatar asked Feb 28 '17 15:02

user1791139


1 Answers

After a lot of research and trying diffferent tooling I finally found the library (HummusJS) capable of doing this. But the question turned out not to be a trivial one. And there are some nuances.

Firs of all the library does not support reading from buffers by default. Here is an adapter that allows to do this:

/*
 PDFRStreamForBuffer is an implementation of a read stream using a supplied array
 @author Luciano Júnior
 */

'use strict';

const EventEmitter = require('events');
class PDFRStreamForBuffer {
    constructor(buffer) {
        this.innerBuffer = buffer;
        this.rposition = 0;
        this.fileSize = buffer.byteLength;
    }

    read(inAmount) {
        let arr = [];

        for (let i = 0; i < inAmount; i++) {
            arr.push(this.innerBuffer[this.rposition + i]);
        }

        this.rposition += inAmount;

        return arr;
    }

    notEnded() {
        return this.rposition < this.fileSize;
    }

    setPosition(inPosition) {
        this.rposition = inPosition;
    }

    setPositionFromEnd(inPosition) {
        this.rposition = this.fileSize - inPosition;
    }

    skip(inAmount) {
        this.rposition += inAmount;
    }

    getCurrentPosition() {
        return this.rposition;
    }
}
module.exports = PDFRStreamForBuffer;

Also by default HummusJS just writes the output to a file. There is a built-in adapter to output it to a writable stream. So in the end of the day I had to use 'memory-streams' module to get the resulting buffer out of the stream.

Once this is clear and you have PDFRStreamForBuffer, you can run this snippet:

'use strict';

const hummus = require('hummus');
const fs = require('fs');
const streams = require('memory-streams');
const PDFRStreamForBuffer = require('./pdfr-stream-for-buffer.js');
const path = require('path');

const getFirstPage = function (buffer) {
    //Creating a stream, so hummus pushes the result to it
    let outStream = new streams.WritableStream();
    //Using PDFStreamForResponse to be able to pass a writable stream
    let pdfWriter = hummus.createWriter(new hummus.PDFStreamForResponse(outStream));

    //Using our custom PDFRStreamForBuffer adapter so we are able to read from buffer
    let copyingContext = pdfWriter.createPDFCopyingContext(new PDFRStreamForBuffer(buffer));
    //Get the first page.
    copyingContext.appendPDFPageFromPDF(0);

    //We need to call this as per docs/lib examples
    pdfWriter.end();

    //Here is a nuance.
    //HummusJS does it's work SYNCHRONOUSLY. This means that by this line
    //everything is written to our stream. So we can safely run .end() on our stream.
    outStream.end();

    //As we used 'memory-stream' and our stream is ended
    //we can just grab stream's content and return it
    return outStream.toBuffer();
};

//Getting the buffer from disk (sync just for demo purpose)
let pdfBuffer = fs.readFileSync(path.join(__dirname, '/original.pdf'));

let firstPageBuffer = getFirstPage(pdfBuffer);

//I wrote it back to disk for testing
fs.writeFileSync(path.join(__dirname, '/result.pdf'), firstPageBuffer);

I did a scientific research to write this snippet. So hope it helps you. :)

like image 106
Antonio Narkevich Avatar answered Sep 22 '22 11:09

Antonio Narkevich