Skip to main content
Inspiring
September 2, 2021
Answered

I need to programmatically flatten AEM pdf's

  • September 2, 2021
  • 3 replies
  • 5376 views

I need to programmatically flatten AEM pdf's.

 

I've searched the other posts here and do not see a solution to my need.  Per the other responses though, I want to add that I am NOT trying to lock down or protect the documents from editing by flattening them. We maintain a local master copy of the documents, so integrity is not the goal.  My need is simply to insure that the live data, watermarks, and signature images in the documents are visible in my end result when the user views the pdf in Chrome's built in pdf reader for example.

 

I can achieve this manually by opening the pdf in Adobe Reader and printing to a pdf (going thru a print to pdf printer driver), but I need a programmatic solution do do this on the fly after my webpage has constructed the document and filled it with data and signatures that I capture.  cfpdf flatten function does not support LiveCycle and AEM documents...

 

Thanks in advance!

 

    This topic has been closed for replies.
    Correct answer BDE60611

    @BDE60611 , I got it to work, using the Ghostscript code. 🙂

     

    When I ran the code on the PDF sample you provided (FinishedDoc9756.pdf), none of the form fields appeared in the flattened PDF. Neither did the signature.

     

    After playing around with it, I discovered a likely cause: insufficient read-write permissions. So I opened your file and used Save-As to make a copy, FinishedDoc9756_unflattened_copy.pdf. When I ran the following code, a flattened PDF was created as expected. I have attached the resulting PDF.

    <cfexecute name="C:/Program Files/gs/gs9.54.0/bin/gswin64.exe" arguments=" -sDEVICE=pdfwrite -dSAFER -dBATCH -dNOPAUSE -dNOCACHE -dQUIET -sOutputFile=C:/Users/BKBK/Desktop/FinishedDoc9756_flattened_copy.pdf C:/Users/BKBK/Desktop/FinishedDoc9756_unflattened_copy.pdf" /> 

     

     

     


    So here is a curiouser thing.

     

    Repeating your step of simply opening the file in Acrobat and saving a new copy makes the form data visible in both Chrome and Edge - without the need to flatten it.

     

    A file comparison between the two yields no diffence in them and I can not discern any permissions differences either.

     

    So there's that.

     

    It looks like I will just have to emply a macro recorder to either open and re-save the pdf, or to print to pdf, in the background.

     

    Thank you for your persistence.

     

    3 replies

    BKBK
    Community Expert
    September 5, 2021

    As @paule12345 and @Dave Watts have suggested, you can programmatically flatten PDFs using Ghostscript.

    In fact, you can do it using just one line of code. 🙂

    <!---
    Ghostscript v9.54.0 downloaded and installed from 
    https://www.ghostscript.com/download/gsdnld.html 
    --->
    <!---
    myUnFlattenedDoc.pdf is the existing AEM PDF.
    myFlattenedDoc.pdf is the flattened PDF created.
    --->
    <cfexecute name="C:/Program Files/gs/gs9.54.0/bin/gswin64.exe" arguments=" -sDEVICE=pdfwrite -dSAFER -dBATCH -dNOPAUSE -dNOCACHE -dQUIET -sOutputFile=C:/Users/BKBK/Desktop/myFlattenedDoc.pdf C:/Users/BKBK/Desktop/myUnFlattenedDoc.pdf" />	
    
    Done flattening

     

    References:
    http://zeroset.mnim.org/2015/01/07/flatten-pdfs-with-ghostscript/
    https://www.dedoimedo.com/computers/pdf-document-flatten.html

     

    BDE60611Author
    Inspiring
    September 9, 2021

    Thanks. 

     

    It works, as in it produces a secondary pdf, but my inserted data and signatures are lost.

    I'm starting to think that I am using the wrong terminology (flattening).

    Should I expect that the field datas in the document should be retained thru the flattening?

    BKBK
    Community Expert
    September 14, 2021

    So here is a curiouser thing.

     

    Repeating your step of simply opening the file in Acrobat and saving a new copy makes the form data visible in both Chrome and Edge - without the need to flatten it.

     

    A file comparison between the two yields no diffence in them and I can not discern any permissions differences either.

     

    So there's that.

     

    It looks like I will just have to emply a macro recorder to either open and re-save the pdf, or to print to pdf, in the background.

     

    Thank you for your persistence.

     


    Hi @BDE60611 , thanks for the update. I am glad to hear that the problem is practically solved.

     

    quote

    A file comparison between the two yields no diffence in them and I can not discern any permissions differences either.

     

    By @BDE60611

     

    There's perhaps a subtle difference in the file meta-data that we are unaware of. But, hell, the problem has been solved.

     

    Anyway, referring to your original question, did you use the Ghostscript code on the copy? Did it produce the correct flattened PDF? If so, then please mark this thread as correctly answered. That will certainly help someone else in future.

    Inspiring
    September 3, 2021

    You might want to take a look at Ghostscript.  We call the ghostscript command line exe from CF to convert PDFs to JPGs, for example.

     

    Community Expert
    September 3, 2021

    That's a good answer too, I think!

     

    Dave Watts, Eidolon LLC

    Dave Watts, Eidolon LLC
    Community Expert
    September 2, 2021

    Have you tried using CFPRINT instead of CFPDF? You'd presumably need to have a PDF printer installed on your CF server too, that you can see with getPrinterList().

     

    Dave Watts, Eidolon LLC

    Dave Watts, Eidolon LLC
    BDE60611Author
    Inspiring
    September 3, 2021

    I have, and the page just hangs I believe because I am targeting "Adobe PDF" as the printer in an attempt to print to file and there is no way to pass the save-as name. This needs to end up as a file, not a physical print.

     

    I have also tried to send the pdf to the browser using cfdocument as a wrapper but the client side browser/viewer is called upon to display it and cannot display the live fields. i.e. no conversion or flattening is done in the process of sending it to the client with cfdocument (and really did not expect to).  It appears that cfdocument is just for the sake of declaring the mime type of what is being sent.

    Community Expert
    September 3, 2021

    OK, I'm pretty sure you can use this. It lets you print from a command line without having to specify a file name. You'll have to buy it, though. I don't know if they have a trial version.

     

    https://www.pdfforge.org/pdfcreator

     

    There's probably a way to use any of the standard "save as PDF" options from Adobe or Microsoft too, but I don't know how you'd automate them from CF.

     

    Dave Watts, Eidolon LLC

    Dave Watts, Eidolon LLC