Hummusjs: "createWriterToModify" doesn't work with dynamic files/stream

Created on 17 Sep 2016  路  4Comments  路  Source: galkahana/HummusJS

I am able to send a new pdf file using PDFStreamForResponse. However, when trying to send a modified file, I get a corrupted pdf.

var hummus = require("hummus");
var express = require("express");
var app = express();

app.get("/", function(req, res) {
  res.writeHead(200, {"Content-Type": "application/pdf"});
  var pdfWriter = hummus.createWriterToModify(__dirname + "/TestMaterials/AddedPage.pdf",
    new hummus.PDFStreamForResponse(res)
  );

  var pageModifier = new hummus.PDFPageModifier(pdfWriter, 0);
  pageModifier.startContext().getContext().writeText(
    "Test Text",
    75, 805,
    {font: pdfWriter.getFontForFile(__dirname + "/TestMaterials/fonts/Couri.ttf"), size: 14, colorspace: "gray", color: 0x00}
  );

  pageModifier.endContext().writePage();
  pdfWriter.end();

  res.end();
});

app.listen(4000);

Reading the wiki gives me the impression that createWriterToModifyonly supports writing to file. Is this the case? If not, how can I use a stream with modified pdf files?

Most helpful comment

sure @edparsons,
just replace
var pdfWriter = hummus.createWriterToModify( __dirname + "/TestMaterials/AddedPage.pdf", new hummus.PDFStreamForResponse(res) );

with
var pdfWriter = hummus.createWriterToModify( new hummus.PDFRStreamForFile(__dirname + "/TestMaterials/AddedPage.pdf"), new hummus.PDFStreamForResponse(res) );

rest of the code should be the same

All 4 comments

it's not that createWriterToModify only supports writing to file, it's just that if you do want to use streams both input and output should be streams (just cause i didn't care to write all 4 options...sorry). (see https://github.com/galkahana/HummusJS/wiki/Modification#creating-a-writer-for-file-modification, 2nd overload).

if you need a stream for file just use new hummus.PDFRStreamForFile (see here https://github.com/galkahana/HummusJS/wiki/Custom-streams#available-streams)

Gal.

Hi Gal, could you write a quick code example of this? I am struggling to implement it myself.

sure @edparsons,
just replace
var pdfWriter = hummus.createWriterToModify( __dirname + "/TestMaterials/AddedPage.pdf", new hummus.PDFStreamForResponse(res) );

with
var pdfWriter = hummus.createWriterToModify( new hummus.PDFRStreamForFile(__dirname + "/TestMaterials/AddedPage.pdf"), new hummus.PDFStreamForResponse(res) );

rest of the code should be the same

is there any file size limit for this. because for me i am trying with 1MB file its not working

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ziram picture ziram  路  3Comments

simone picture simone  路  8Comments

KPrabs106 picture KPrabs106  路  5Comments

JensWinter picture JensWinter  路  5Comments

nnnikolay picture nnnikolay  路  7Comments