Automator script to convert RTF to PDF

Hi I had an Automator script set up as a service, that used to work on MacOS prior to El Capitan. I could right click on RTF file, choose the script, and it would convert RTF (or DOCX) to PDF file.


The source of the script: (it's a shell script)

https://jacobsalmela.com/2016/03/12/convert-docx-to-pdf-natively-with-a-right-click/


This has stopped working now in El Capitan. It does create a PDF file, but it is empty of content, 0 bytes in size, and can't be opened.


Anyone know how to get this working in El Capitan and beyond now please?

MacBook Pro 15″, macOS 10.15

Posted on Sep 29, 2021 1:56 AM

Reply
Question marked as Top-ranking reply

Posted on Sep 29, 2021 7:32 PM

Here is a Quick Action that uses a single action (run shell script) to convert specific types of files selected in the Finder to PDF. It is written to use the file's path as the output directory, so selecting a file on the Desktop will see the PDF bearing the original filename written to the Desktop.


Here is the Quick Action:



and the code to copy and paste into the blank Run Shell Script action:


#!/bin/zsh

: <<'COMMENT'
Quick Action to convert selected files in the Finder to PDF. Default output
directory is the same as the input file.

Zsh file modifers: a - absolute path
                   h - just the path, not the file name

Tested: macOS 11.6, Zsh 5.8
Dependency: Needs LibreOffice Suite installed.
VikingOSX, 2021-09-29, Apple Support Communities, No warranties expressed or implied.
COMMENT

source $HOME/.zshrc || :

for f in "${@}";
do
	case "${f}" in
		*.(txt|rtf|doc|docx|odt))
			echo ${f} > ~/Desktop/rpt.txt
			# convert document to PDF and place it in the same folder location
			/Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to pdf \
				--outdir "${f:a:h}" "${f:a}" 2> /dev/null
			;;
		*.(pdf|rtfd|webarchive))
			continue;;
	esac
done
exit 0


I named by Quick Action, To PDF. You must have the current LibreOffice Suite installed for this QA to work. Presently, LibreOffice needs macOS 10.12 (Sierra) or later.

5 replies
Question marked as Top-ranking reply

Sep 29, 2021 7:32 PM in response to JDfunky

Here is a Quick Action that uses a single action (run shell script) to convert specific types of files selected in the Finder to PDF. It is written to use the file's path as the output directory, so selecting a file on the Desktop will see the PDF bearing the original filename written to the Desktop.


Here is the Quick Action:



and the code to copy and paste into the blank Run Shell Script action:


#!/bin/zsh

: <<'COMMENT'
Quick Action to convert selected files in the Finder to PDF. Default output
directory is the same as the input file.

Zsh file modifers: a - absolute path
                   h - just the path, not the file name

Tested: macOS 11.6, Zsh 5.8
Dependency: Needs LibreOffice Suite installed.
VikingOSX, 2021-09-29, Apple Support Communities, No warranties expressed or implied.
COMMENT

source $HOME/.zshrc || :

for f in "${@}";
do
	case "${f}" in
		*.(txt|rtf|doc|docx|odt))
			echo ${f} > ~/Desktop/rpt.txt
			# convert document to PDF and place it in the same folder location
			/Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to pdf \
				--outdir "${f:a:h}" "${f:a}" 2> /dev/null
			;;
		*.(pdf|rtfd|webarchive))
			continue;;
	esac
done
exit 0


I named by Quick Action, To PDF. You must have the current LibreOffice Suite installed for this QA to work. Presently, LibreOffice needs macOS 10.12 (Sierra) or later.

Sep 29, 2021 6:40 AM in response to JDfunky

After Snow Leopard, perhaps a little bit longer, Apple removed the HTML and RTF conversion features from cupsfilter, which is why it now fails with the following message in Big Sur 11.6, and Catalina:




Now, if you want to convert .DOC/X, or RTF to PDF from Automator (in one step) and you do not want to open the source documents in Pages and export them to PDF, you will need a third-party application to achieve this. The free LibreOffice Suite has a command-line tool that you can reference from an Automator Quick Action (run shell script) that will convert any selected, previously mentioned format to PDF. They will either be converted in the same filesystem location, or you can assign a designated folder for the resultant PDF output.


Do you want to see an Automator solution that uses the LibreOffice Suite command-line tool in an Automator Quick Action?





This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Automator script to convert RTF to PDF

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.