node.js - How do I replace a string in a PDF file using NodeJS? -


i have template pdf file, , want replace marker strings generate new pdf files , save them. what's best/simplest way this? don't need add graphics or fancy, simple text replacement, don't want complicated.

thanks!

edit: found hummusjs, i'll see if can make progress , post here.

i found question searching, think deserves answer. found answer brightide here: https://github.com/galkahana/hummusjs/issues/71#issuecomment-275956347

basically, there powerful hummus package uses library written in c++ (crossplatform of course). think answer given in github comment can functionalized this:

var hummus = require('hummus');  function replacetext(sourcefile, targetfile, pagenumber, findtext, replacetext) {         var writer = hummus.createwritertomodify(sourcefile, {             modifiedfilepath: targetfile         });         var modifier = new hummus.pdfpagemodifier(writer, pagenumber);         var sourceparser = writer.createpdfcopyingcontextformodifiedfile().getsourcedocumentparser();         var pageobject = sourceparser.parsepage(pagenumber);         var textobjectid = pageobject.getdictionary().tojsobject().contents.getobjectid();         var textstream = sourceparser.querydictionaryobject(pageobject.getdictionary(), 'contents');         //read original block of text data         var data = [];         var readstream = sourceparser.startreadingfromstream(textstream);         while(readstream.notended()){             array.prototype.push.apply(data, readstream.read(10000));         }         var string = new buffer(data).tostring().replace(findtext, replacetext);          //create , write our new text object         var objectscontext = writer.getobjectscontext();         objectscontext.startmodifiedindirectobject(textobjectid);          var stream = objectscontext.startunfilteredpdfstream();         stream.getwritestream().write(strtobytearray(string));         objectscontext.endpdfstream(stream);          objectscontext.endindirectobject();          writer.end(); }  // replacetext('source.pdf', 'output.pdf', 0, /replaceme/g, 'my new custom text'); 

Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -