title: How can I use Itext in PHP?
body: <p><a href="https://itextpdf.com/en" rel="nofollow noreferrer">Itext</a> is a powerful toolkit for <em>PDF generation</em>, <em>PDF programming</em>, <em>handling &amp; manipulation</em>. <strong>Java</strong> sample: </p>
  
  <pre><code>public void processPDF(String src, String dest) {
  
  try {
  
    PdfReader reader = new PdfReader(src);
    PdfArray refs = null;
    PRIndirectReference reference = null;
  
    int nPages = reader.getNumberOfPages();
  
    for (int i = 1; i &lt;= nPages; i++) {
      PdfDictionary dict = reader.getPageN(i);
      PdfObject object = dict.getDirectObject(PdfName.CONTENTS);
      if (object.isArray()) {
        refs = dict.getAsArray(PdfName.CONTENTS);
        ArrayList&lt;PdfObject&gt; references = refs.getArrayList();
  
        for (PdfObject r : references) {
  
          reference = (PRIndirectReference) r;
          PRStream stream = (PRStream) PdfReader.getPdfObject(reference);
          byte[] data = PdfReader.getStreamBytes(stream);
          String dd = new String(data, "UTF-8");
  
          dd = dd.replaceAll("@pattern_1234", "trueValue");
          dd = dd.replaceAll("test", "tested");
  
          stream.setData(dd.getBytes());
        }
  
      }
      if (object instanceof PRStream) {
        PRStream stream = (PRStream) object;
  
        byte[] data = PdfReader.getStreamBytes(stream);
        String dd = new String(data, "UTF-8");
        System.out.println("content----&gt;" + dd);
        dd = dd.replaceAll("@pattern_1234", "trueValue");
        dd = dd.replaceAll("This", "FIRST");
  
        stream.setData(dd.getBytes(StandardCharsets.UTF_8));
      }
    }
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    stamper.close();
    reader.close();
  }
  
  catch (Exception e) {
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The question is how I can use this <strong><em>toolkit for PHP</em></strong> or there is <em>any <strong>PHP toolkit</strong> like this</em>?
  I am using <strong>Yii2 Framework</strong> and just want to <em>replace text inside</em> a certain file (<strong>.doc, .docx, .pdf</strong>)</p>
  
  <p>Original file:
  <a href="https://i.stack.imgur.com/NQATH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NQATH.png" alt="Original file"></a>
  Expected file after editing:
  <a href="https://i.stack.imgur.com/RT4hh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RT4hh.png" alt="Expected file after editing"></a></p>