0

Itext is a powerful toolkit for PDF generation, PDF programming, handling & manipulation. Java sample:

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 <= nPages; i++) {
    PdfDictionary dict = reader.getPageN(i);
    PdfObject object = dict.getDirectObject(PdfName.CONTENTS);
    if (object.isArray()) {
      refs = dict.getAsArray(PdfName.CONTENTS);
      ArrayList<PdfObject> 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---->" + 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) {
}

}

The question is how I can use this toolkit for PHP or there is any PHP toolkit like this? I am using Yii2 Framework and just want to replace text inside a certain file (.doc, .docx, .pdf)

Original file: Original file Expected file after editing: Expected file after editing

  • 1
    You can use the mPDF lib for this in PHP: https://mpdf.github.io/ This lib provide a method `OverWrite()` with which you can replace text in PDF files. – CodyKL Dec 09 '19 at 04:48
  • I've tried, It did create a new file, however, the content remains the old one :( – Ngô Văn Quyền Dec 09 '19 at 07:03
  • Does this answer your question? [How to use iText java PDF library with PHP?](https://stackoverflow.com/questions/4390675/how-to-use-itext-java-pdf-library-with-php) – Amedee Van Gasse Dec 09 '19 at 08:20
  • yeah, I've already read that one! My question is more specific, by the way. – Ngô Văn Quyền Dec 09 '19 at 08:26
  • A word of warning: The iText code in your question is an example that **won't work for generic PDFs**. It only "works" for very simple PDFs and only for search and replacement text with characters as used in Western European languages. Furthermore, your explicit and implicit use of `UTF-8` here most likely damages more than necessary as PDF content streams are not UTF-8 encoded. – mkl Dec 09 '19 at 10:44
  • @mkl In fact, I only use it to replace phone numbers and emails which are used with Latin strings so it's really ok for this project. I've tried Itext with Java, and it runs perfectly. The problem now is to search text from pdf and replace or hide it. :P – Ngô Văn Quyền Dec 09 '19 at 11:02

0 Answers0