Copy link to clipboard
Copied
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!
I ran your PDF through the CFPDF tag (CF2018) and it did flatten the PDF minus the signature squiggle.
<cfpdf action="write"
flatten="yes"
source="d:\myserver\FinishedDoc9756.pdf"
destination="d:\myserver\finisheddoc_flat.pdf"
overwrite="yes">
@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 th
...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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
That's a good answer too, I think!
Dave Watts, Eidolon LLC
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
No, I think "flattening" is the right word, you don't want them to be separate layers or objects in your new PDF. I suspect there's a switch in Ghostscript or PDF Creator to get you where you want to go.
Dave Watts, Eidolon LLC
Copy link to clipboard
Copied
@BDE60611 , the code I gave was just a proof-of-concept, to show it can be done. You have to apply the correct switches for your particular case, as Dave suggested.
For example, if your PDF requires a password, you should add the switch -sPDFPassword=your_pdf_password. You will find the switches in the link that Paule12345 provided.
Copy link to clipboard
Copied
I'm not familiar with AEM, but taking a quick look at the documentation, I found this regarding the AEM Output service. One of the functions is to create a "non-interactive PDF" which is what I would call a "flattened" PDF.
I'm wondering if the form-filled PDF's you're generating are being assembled in the client's browser using AEM client-side browser code and not actually assembled and saved to disk on the server? That would explain why the form field data disappears when you try to "flatten", because the server side master document is not actually being updated on the server side.
We use the CFPDF tag all the time to form-fill, apply stamps, and flatten the resulting PDF, so I think there's something else going on with the way AEM operates.
Copy link to clipboard
Copied
>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.
Copy link to clipboard
Copied
The problem is, in part, that LiveCycle forms aren't actually PDFs. They're XFA (XML Forms Architecture if I remember right) and Acrobat has the ability to render them as PDFs. I assumed that PDF Creator (and Ghostscript) would let you print an XFA form as a PDF. Maybe not. If not, there's a specific LiveCycle component that will, I'm sure, but it's not included with CF.
Dave Watts, Eidolon LLC
Copy link to clipboard
Copied
Well the first thing I notice is when I right click save to disk, your "pdf" is actually an HTML document with a .pdf extension. Open the "pdf" in notepad and it's HTML text.
Copy link to clipboard
Copied
This forum software sucks, can't see how to edit my reply, but look at the HTML and it's full of AEM related javascript code. I think you need to look into the AEM Output service I mentioned above, to get you an actual PDF file.
Copy link to clipboard
Copied
Nevermind, thanks to this forum software when I right clicked on the link it saved the forum HTML and not the document. Doh.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
@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" />
Copy link to clipboard
Copied
Awesome sauce! I'll give this another try. Thanks!
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Hi @BDE60611 , thanks for the update. I am glad to hear that the problem is practically solved.
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.
Copy link to clipboard
Copied
The GS script works to flatten the PDF, but only after breathing on the file (opening and saving with Adobe Acrobat or Reader DC).
Will need to employ a Macro app to facilitate this. Was hoping for a cleaner / lower impact solution.
So, a reluctant agreement that this is solved.
Thanks for all replies!
Copy link to clipboard
Copied
Windows Script Host FTW