galkahana / HummusJS

Node.js module for high performance creation, modification and parsing of PDF files and streams
http://www.pdfhummus.com
Other
1.14k stars 169 forks source link

pdfWriter.end not closing #258

Open GoNode5 opened 6 years ago

GoNode5 commented 6 years ago

I have a problem that this (simplified) code seems not to close the created c.pdf. If I try to open the pdf while the server/nodejs is running, I get a an error with Foxit Reader: Encountered a sharing violation while accessing. As soon as I close the node.js, the problem is gone. node v8.7.0 / windows 10

const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;

var hummus = require('hummus');

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World!\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

let inStream1 = new hummus.PDFRStreamForFile('a.pdf');
let inStream2 = new hummus.PDFRStreamForFile('b.pdf');
let outStream = new hummus.PDFWStreamForFile("c.pdf");
let pdfWriter = hummus.createWriterToModify(inStream1, outStream);

   pdfWriter.appendPDFPagesFromPDF(inStream2);
   pdfWriter.end();
galkahana commented 6 years ago

you should call outStream.close() for the file pointer to be release (see here for reference). Note that the method is async, and allows you to provide a callback for when the file is truly closed.

GoNode5 commented 6 years ago

thanks for your help (and wonderful component). I did eventually find it myself. inStream1.close(function(){}); inStream2.close(function(){}); outStream.close(function(){}); Not very elegant... Not sure if inStreams should be closed as well?