Skip to main content
Inspiring
September 2, 2021
Answered

I need to programmatically flatten AEM pdf's

  • September 2, 2021
  • 3 replies
  • 5373 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
    Braniac
    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
    Braniac
    September 11, 2021

    >We use the CFPDF tag all the time to form-fill, apply stamps, and flatten the resulting PDF...

    This is what I am doing as well.  All server side. My resultant file has everything I expect to see in it, if viewed with Adobe Reader DC.  PostProcessing that file with GS renders a new pdf missing the form data.

     

    >I think there's something else going on with the way AEM operates...

    Yes, ColdFusion does not support flattening of LiveCycle/AEM forms.

    https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-tags/tags-p-q/cfpdfform.html

     

    I have read the GS documentation looking for a flattening command/flag and find none - it would seem that flattening "is" just what it does as part of the processing.. Please correct me if I am wrong.

    https://www.ghostscript.com/doc/current/Use.htm

     

    I am executing the following, based on suggestion above and what I see in the docs.  Note, I have removed the batch and quiet flags so I my view the processor window to see if there are any errors reported (and none are).

    "C:/Program Files/gs/gs9.54.0/bin/gswin64.exe" -sDEVICE=pdfwrite -dPDFSETTINGS=/default -dSAFER -dNOPAUSE -sOutputFile=C:\temp\pdf\FinishedDoc9756flat.pdf C:\temp\pdf\FinishedDoc9756.pdf

     

    I am attaching a test pdf that I am trying to flatten that contains form data so one of you might put it thru its' paces.  Thanks kindly.

     


    @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" /> 

     

     

     

    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.

     

    Inspiring
    September 3, 2021

    That's a good answer too, I think!

     

    Dave Watts, Eidolon LLC

    Dave Watts, Eidolon LLC
    Inspiring
    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.

    Inspiring
    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