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