title: Error replacing text in PDF with NodeJS and hummus
body: <p><a href="https://stackoverflow.com/questions/36778637/how-do-i-replace-a-string-in-a-pdf-file-using-nodejs">How do I replace a string in a PDF file using NodeJS?</a> has a solution to replace text in a PDF. With the same code, I have a puzzling issue: the text is replaced in the source code of the PDF but does not render. The relevant lines, adapted from the above solution, are:</p>
  <pre class="lang-js prettyprint-override"><code>  console.log(replaceText);
    var string = new Buffer(data).toString().replace(findText, replaceText);
    console.log(string);
  </code></pre>
  <p>The console shows that it is replaced in the source string of the PDF:</p>
  <pre>
  /TT0 1 Tf
  65.5689 -24.5097 24.5097 65.5689 363.9941 762.3682 Tm
  (e)Tj
  61.1539 -34.0617 34.0617 61.1539 381.1689 756.6411 Tm
  (n)Tj
  54.8214 -43.5272 43.5272 54.8214 408.6333 741.0947 Tm
  (d)Tj
  48.8331 -50.153 50.153 48.8331 426.3779 726.999 Tm
  (a)Tj
  52 0 0 52 75.8203 226.9756 Tm
  (abcdefghijklmnopqrstuvwxyz)Tj
  33 0 0 33 25.8203 302.9756 Tm
  (E)Tj
  (ste cheque-prenda, para:)Tj
  1.818 -7.152 Td
  (www.emocoes.org/abcdefghijklmnopqrstuvwxyz)Tj
  ET
  </pre>
  <p>and the PDF looks like this:</p>
  <p><a href="https://i.stack.imgur.com/l3Bra.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/l3Bra.png" alt="result in PDF" /></a></p>
  <p>The K, X, and Y are missing in this case. Opening the file in Adobe Illustrator shows they are still there behind other letters:</p>
  <p><a href="https://i.stack.imgur.com/GLXu7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GLXu7.png" alt="result in Illustrator" /></a></p>
  <p>I could not find a definite pattern: sometimes H and J are also missing with other replacemenet strings, and the missing letters are different with other fonts (I tested Open Sans and Times New Roman).</p>
  <p>What is the problem, and how can I fix it?</p>
  <p>My code is:</p>
  <pre class="lang-js prettyprint-override"><code>function customizeVoucher(findText, replaceText) {
    var sourceFile = path.join(__dirname, &quot;../private/vouchers/custom-old.pdf&quot;);
    var link = &quot;/vouchers/cheque-prenda-&quot; + replaceText + &quot;.pdf&quot;;
    var targetFile = path.join(__dirname, &quot;../private&quot; + link);
    var pageNumber = 0;
    
    var writer = hummus.createWriterToModify(sourceFile, {
      modifiedFilePath: targetFile,
      log: path.join(__dirname, &quot;../hummus.md&quot;)
    });
    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 the original block of text data
    var data = [];
    var readStream = sourceParser.startReadingFromStream(textStream);
    while(readStream.notEnded()){
      Array.prototype.push.apply(data, readStream.read(10000));
    }
    console.log(replaceText);
    var string = new Buffer(data).toString().replace(findText, replaceText);
    console.log(string);
  
    // Create and 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();
  
    return link;
  }
  </code></pre>
  <p>and the source PDF is <a href="https://www.dropbox.com/s/cegf2knaq1ygnm7/custom-old.pdf?dl=0" rel="nofollow noreferrer">here</a>.</p>