marcbachmann / node-html-pdf

This repo isn't maintained anymore as phantomjs got dreprecated a long time ago. Please migrate to headless chrome/puppeteer.
MIT License
3.56k stars 544 forks source link

How to output as string from a stream ? #62

Open sarathsaleem opened 9 years ago

sarathsaleem commented 9 years ago

I cannot use the pipe method of a stream, instead I want o convert this stream as a valid pdf string.

So how to convert the stream data into a string.

pdf.create(html).toStream(function(err, stream) {

       //  how to convert this stream as a valid pdf "string" so that i can use the res as 

    res.contentType("application/pdf");
    res.setHeader('Content-disposition', 'attachment;filename=test.pdf');
    res.setHeader('Content-Length', string.length);
    res.send(string);
});
marcbachmann commented 9 years ago

You can use toFile(function(err, file)) and fs.readFile(file.path, 'utf8', callback)

marcbachmann commented 9 years ago

Sorry, I didn't read you issue title :wink: Here's the answer:

chunks = [] 
stream.on('data', function addToChunks (chunk) { chunks.push(chunk) })
stream.on('end', function onEnd () { res.send(Buffer.concat(chunks).toString()) })

I guess res.send(buffer) should work. so you can just use Buffer.concat(chunks) without toString.

sarathsaleem commented 9 years ago

By this method I am able to get the response as PDF. But the file is corrupted. I this the pdf jheader and body part has to be handled in different way.

by analyzing the corrupted file.

Analysis terminated; corruptions found.
Page  ObjNo  Description
   0      0  The 'xref' keyword was not found or the xref table is malformed.
   0     10  The "Length" key of the stream object is wrong.
   0     10  Error in Flate stream: data error.
   1      0  The file is corrupt and cannot be repaired. Some of the contents can possibly be recovered.
   1     10  Error in Flate stream: data error.

.toString('utf8') also didn't work.

harindaka commented 8 years ago

Same issue here. The string produced via stream.on('data' and stream.on('end' method is not valid. A simple test can be where you read the file produced by toFile method into bas64 and compare it with the aforementioned method's buffer.toString('base64'). Content is not the same. Any resolution to this?

sblantipodi commented 7 years ago

I have the same problem, is there someone who succeded in generating the PDF in base64 format string?

sblantipodi commented 7 years ago

solved this way `pdf.create(htmlTemplate.body, options).toStream(function(err, stream) {

    var chunks = [];
    stream.on('data', function(chunk) {
        chunks.push(chunk);
        console.log('chunk:', chunk.length);
        });
    stream.on('end', function() {
           var result = Buffer.concat(chunks);
           logger.debug(util.format('Received request: %s', result.toString('base64')));
           console.log('final result:', result.toString('base64'));
        });
});`
NikhilMutkekar commented 6 years ago

@sblantipodi i was able to convert it to base64 encoding. But when i decoded using atob(result.toString('base64')), data seem to be corrupt. Any reason why?