2019年8月3日 星期六

Flatten PDF with Automator

Download v1.0 (For single-page PDFs)
Download v2.0 (Please see the update below)

PDF (Portable Document Format) is a popular document format, which can be treated as a kind of digital paper, including contents (e.g. texts and images) and very stable / unchangeable layout. Many businessmen take it as a "safe" format, and researchers use it to compose academic documents, because many productivity softwares can export vector-graphic images in PDF [1].

However, PDF is too stable to modify it, so all we can do is "add" some notes on PDF files. Annotating is a popular feature supported by most PDF viewing softwares, such as Acrobat Reader, macOS Preview, PDF Expert and so on. In addition,  these notes are always re-editable, just like that we can really "modify" the PDF files. But, adding notes is just to cover a virtual layer on the original PDF, totally different from real editing, what if you come across some softwares without annotating supports? When we import PDF files into these softwares, our notes on PDF would be totally lost. For example, the popular academic document composer LaTeX doesn't include the PDF notes in the compiled file, and Keynote doesn't support it, either.

On the other hand, sometimes we need our notes on PDF don't be modified anymore, e.g. some filled formal forms in PDF. To solve this problem, we need to merge notes into the original file content, which is called "PDF flattening".

There are many "Pro" PDF softwares supporting the "flatten" feature, such as Adobe Acrobat and PDF Expert, but they cost money. macOS builds-in very powerful PDF supports, but we cannot find any similar features in Preview. Many people have asked this question on line, but the answers are always the same (link 1link 2): "Print to PDF" (or "Export as PDF", the same result), but....

PDF (Portable Document Format) 是一個熱門的文件格式,它也常被視為數位版的紙張,裡面夾帶著內容 (文字及圖片) 及非常穩定 / 不可修改的文件排版。很多商務人士拿它當作是一種 "安全" 的格式,而學術研究者用它來製作學術文檔,因為很多文書軟體輸出的向量圖格式是 PDF [1]。

然而,由於 PDF 穩定到不可修改,所以我們能做的就是在上面加註解。大部分 PDF 的閱讀軟體都支援註解功能,例如 Acrobat Reader,、macOS 預覽程式、PDF Expert 等等。此外,這些註解是可再次修改的,就像是我們真的可以修改 PDF 一樣。可是,加註解只是在原本的 PDF 上覆蓋一層虛擬層,跟實際的文檔編輯完全不同,如果遇到不支援註解的軟體該怎麼辦?當我們匯入 PDF 檔到這些軟體,註解將會全部消失。舉個例子,熱門學術文件排版工具 LaTeX 就不支援,其編譯出來的檔案完全不包含註解,而 Keynote 也是一個不支援的例子。

另一方面,有時我們需要我們在 PDF 裡留下的註解不會被再次修改,例如一些 PDF 格式的正式表格。為了解決此問題,我們需要將註解及原始檔案的內容融合在一起,稱為 PDF 的 "平面化" (Flatten)。

市面上有很多 "專業" 軟體能支援 "平面化" 的功能,像是 Adobe Acrobat 及 PDF Expert,但是他們都要花錢。macOS 內建強大的 PDF 支援,但是 Preview 沒有類似的功能。很多人在網上求解,但答案總是一樣 (連結 1連結 2):"列印成 PDF" (或是 "輸出成 PDF",結果都一樣),但是 .....

Printing preview 
預覽列印

Yes, the PDF file is flattened, but you need to crop it manually if the file is not saved in A4 size or other printing sizes, sucks. How about other thirty party tools? Some people suggest pdf2ps, ImageMagick (I tried, but failed), cups-pdf, pdftk (Windows software?), there are all a lot of hassles. Finally, I found an Automator action "New PDF from Images" which supports this feature, but it is not very user-friendly, so I built "Flatten PDF" Automator workflow.

是的,這 PDF 檔是被平面化了,但若它的尺寸不是 A4 或其他列印大小,就要手動去裁切,爛斃了。那其他第三方工具如何?有人推薦 pdf2ps, ImageMagick (我試過,失敗), cups-pdf, pdftk (Wondows 軟體?),總之都太麻煩。最後,我發現一個 Automator 動作 "New PDF from Images" 支援此功能,但不是很好用,所以我寫了 "Flatten PDF" 工作流程。


How to use

1. Drag and drop the files into "Get Specified Finder Items" action. The files in different directories are supported.

1. 將檔案拖拉到 "Get Specified Finder Items" 動作中,在不同資料夾的檔案也可以。


2. Click the upper-right "Run" button to run this workflow, and then the flattened version will show up next to the original file.

2. 按下右上角的 "Run" 按鈕以執行工作流程,而後平面化的檔案將會出現在原始檔案旁邊。


3. This workflow supports the integration with other Automator actions. If you don't like the name (original name + "_flattened"), there is no need to modify my AppleScript codes, just use "Replace Text" feature of "Rename Finder Items" action as follow:

3. 這個工作流程支援與其他 Automator 動作串連。如果不喜歡加了 "_flattened" 的檔名,無須修改我的 AppleScript 程式碼,使用 "Rename Finder Items" 中的 "Replace Text" 功能即可:

Change the filenames *_flattened to *superman 
改變檔名從 *_flattened 至 *superman

If there is any feedback, please leave your comment or email me, thanks!

If you tend to ask me the iOS solution with Siri Shortcut, I can tell you the answer: Not exist (I hate using webAPI to do local things, so I haven't checked the online solution). Installing a third party App to flatten PDF seems to be the only, and more convenient solution on iOS.

如果有任何反饋,請留言或寄信給我,多謝!

如果要問我有沒有 Siri 捷徑的 iOS 解決方案,我可以告訴你答案:"沒有" (我討厭用 WebAPI 去處理本機檔案的問題,所以我沒有確認是否有線上的解決方案)。為了平面化 PDF,在 iOS 上安裝第三方 App 似乎是唯一且較方便的方案。


20190823 Update: v2.0 release. The issue for the multi-page PDF support is fixed. However, the app in this version cannot be used directly, a "shortcut" file needs to built as below:

20190823 Update: 2.0 版釋出。多頁 PDF 的支援問題已修正。然而這個版本的應用程式不能直接使用,需要製作一個 "捷徑" 檔,方法如下:


The shortcut file can be a workflow, an application or a service. The important part is "Run Workflow" action, please follow the setting in the above figure. By the way, other Automator actions are still supported to modify the output files.

捷徑檔可以是工作流程、應用程式或服務,"Run Workflow" 是其中的重點,請依照上圖設定之。附帶一提,其他 Automator 動作仍然可以串連使用,以對輸出的檔案進行後續的處理。



20190806 Update:
Flatten PDF for iOS

Mmm.... Although I don't like it, I searched for the online solutions and got some results. Surprisedly, I found only very few websites can achieve "successful" PDF flattening [2]. Here I suggest Flatten PDF of freetoolonline.com, which built an amazing mobile website, so there is no need to make the Siri Shortcut for it. Before using it, please make sure your document is not confidential, if there is any privacy or security concern, don't use any online solution.

嗯 ..... 真香。雖然我不喜歡這些玩意,但還是上網找了一些線上解決方案。令人驚訝的是,我發現很少網站能做到 "成功" 的 PDF 平面化 [2]。在這裡我推薦使用 freetoolonline.com 的 Flatten PDF,它製作了非常好用的移動版網站,所以沒有必要製作 Siri 捷徑了。使用之前,請確定你的文件不是機密文件,如果有任何隱私或安全性的考量,不要使用任何線上解決方案。


The website is straightforward: 1. choose the PDF file you want to upload; 2. select "GhostScript" ("ImageMagick" flattens PDF by converting the file to image(raster graphics), which causes the PDF won't be a vector graphics anymore), and then click "Flatten"; 3. download the flattened version file.

網站的使用方式非常直觀:1. 選擇要上傳的 PDF 檔;2. 選擇 "GhostScript" ("ImageMagick" 是以將檔案轉為點陣圖檔的方式去實現平面化,此舉造成該 PDF 不再是向量圖檔),然後按下 "Flatten";3. 下載平面化的版本。



Notes
1. I know the symbol of vector graphics is SVG (Scalable Vector Graphics), but I hardly see anyone using it (maybe it lives in the free software community only), and many productivity softwares don't build-in SVG support. Therefore, I always use PDF.

2. Let me list the other websites I tried as below:

1. 我知道向量圖檔的代表是 SVG (Scalable Vector Graphics),但是我甚少看到任何人用它 (也許它只活在自由軟體社群裡),而且很多文書軟體都不內建支援 SVG,所以我總是用 PDF。

2. 以下是我試過的其他網站:

2 則留言:

  1. Hi, So it seems that since Mac OS ventura this stopped working properly. It now loses a lot of quality when running the action "New PDF from Images", can you please work on a fix? Thanks

    回覆刪除
    回覆
    1. Sorry about my late reply. "Flatten PDF" is an Automator application I use frequently. However, the version of Mac OS of my old Mac is Catalina (10.15), so I haven't found this issue util you gave me this feedback.

      Today I went to some Apple authorized reseller to check it. Yes, the quality becomes very bad after "New PDF from Images" works on that file.

      Unfortunately, Automator is a system-level function, and Apple seems not to maintain its functionality, because Apple hopes Shortcut to take over its work, but Shortcut has no features to perform this function so far.

      So, I will manage to use another PDF tools to approach a similar feature and integrate it to my script, but I need time.

      Thank you for your reminder. I am not sure that when I could get my new M2 Mac mini (it might be the beginning of this April, based on Taiwan page of Apple.com). I will start the development to make a new one without "New PDF from Images" component, and I hope the result could be released in the near future.

      Thank you again.

      刪除