Convert to PDF

2009
05.06
PDF

PDF

I was astonished not to discover an easy way to convert text files to PDF with automator! Yes, I found some evidence of a past action which is now gone, here is a new implementation and written in Ruby.

The truth

Mac users are blessed by the divine File->Print->Save As PDF menu item! Every application that is able of printing its content can be Saved as PDF. But for some reasons there isn’t a programatically way of using this feature.

But as long as your only need is to convert f.ex text files, OS X comes with a handy command line tool ‘cupsfilter’, which takes a file as input and throws a PDF file back.

Ruby Elixir

Here is the Automator Ruby code that accomplish the painful task of batch processing files to PDF.

ARGF.each do |f|		# for each Automator input
	f.chop! 	# get rid of the \n charachter

	# split filename from the file extention
	ext = File.extname(f)
	basename = File.basename(f, ext)

	# get the ouput folder
       o = ENV['output']
	output_dir = File.expand_path(o.nil? ? "~/Desktop" : o)

	# get the new name
	new_file = "#{output_dir}/#{basename}.pdf"

	# execute cupsfilter
        # don't ever forget to quote file paths it's a deadly mistake
        # thanks to steve pointing this out!

        #BAD no quotes around #{f} => `cupsfilter #{f} 2> /dev/null > "#{new_file}"`

        `cupsfilter "#{f}" 2> /dev/null > "#{new_file}"`

	# check if file  converted
  	unless $?.success? then
    	File.delete(new_file) if File.exists?(new_file)
    	$stderr.puts "Cannot convert #{basename}#{ext}"
    	exit 1
  	else
    	puts new_file
  	end
end

Download

No need for additional software on Mac to succeed in performing vital tasks.

Download Convert To PDF

Related posts:
  1. Convert to PDF Version 1.1 A minor update for a developer a major improvement for...
  2. Doc to pdf with Automator As requested here is one possible way to convert doc...
  3. Convert to PDF Version 1.2 A long time has passed and our favorite OS has...
  4. Flatten Movies Flatten Movies is an Automator action to flatten every movie...
  5. Flatten Movies Version 1.1 Flatten Movies is an Automator action to flatten every movie...

Tags: , , , , , , , ,

33 Responses to “Convert to PDF”

  1. Juergen Venne says:

    Thank you for making this public.
    Very handy!

    Juergen

  2. Uldis says:

    Hey, thanks.
    Just a question – it does work just for text files, right? Just had an idea to make PDF out of collection of images

    • William says:

      I left this information vague on purpose in my description as I’m not a cups expert. I just run a few tests with JPEG and PNG files; it works. The manual page for cupsfilter says: “The standard filters supports many types of image files” I assume this to be true, but I cannot tell you for sure which file types are supported on your system. You can look in the cupsfilter configuration file ‘/etc/cups/mime.convs’.

      On my leopard installation I find that it should filter the following types of files:

      application/pdf
      application/postscript
      application/vnd.hp-HPGL
      application/x-cshell
      application/x-csource
      application/x-perl
      application/x-shell
      text/plain
      text/html
      image/gif
      image/png
      image/jpeg
      image/tiff
      image/x-bitmap
      image/x-photocd
      image/x-portable-anymap
      image/x-portable-bitmap
      image/x-portable-graymap
      image/x-portable-pixmap
      image/x-sgi-rgb
      image/x-xbitmap
      image/x-xpixmap
      image/x-sun-raster
      image/x-sgi-rgb
      image/x-xbitmap
      image/x-xpixmap
      image/x-sun-raster
      application/vnd.cups-postscript
      application/octet-stream

  3. Uldis says:

    @William
    oh, thanks a lot once again!

  4. steve hire says:

    I’m trying to make this work. I’ve double-clicked the action and installed it in Automator’s Library. I have created and saved the workflow as a finder plug-gin. But when i use it, nothing happens.

    What am I not getting???!!! Can someone explain to me what I’m too dense to see?

    • William says:

      I cannot reproduce your error. By “the workflow” do you mean, you saved the Sample Workflow? By starting with en empty Workflow, I add only the Convert to PDF action, I switch on the option of the action “Show when run”( to actually being asked for the destination if you want that) and save it as Finder plugin. I get the expected result.

  5. steve hire says:

    @William

    thanks for responding William. I did not save your sample workflow. However, I believe I recreated it exactly, i.e. “get specified finder item” “convert to PDF”, and saved as a finder plug-in. It doesn’t quit or show an error. However, it also doesn’t do any conversion.

    I tried what you said and created a workflow consisting of only “convert to PDF with “show when run”. Again, it seems to do it’s trick. But i can find no created PDF.

    I want to make sure I understand. I highlight some text object in the finder, control cllck and scroll to my automator actions, click off “convert to PDF” and I should find a PDF either next to my original or replacing that original, correct?

    When I do it, I only find my original file, no PDF

    thanks again for your attention, and tryng to help.

    I’d really like to make this work, but I am at times a bit dense.

    • William says:

      The action saves the PDF to the location that was chosen in Automator, at the moment you created your Finder plugin. The default location of a new dropped action should be ‘~/Desktop’. So if you run the action on a file named ‘foo.txt’ in your Document folder. You should find a file ‘foo.pdf’ on your Desktop. If a file ‘foo.pdf’ already existed on your Desktop it gets overwritten.

      You can select the destination folder in Automator, so every time your run your workflow it will always save the PDF to the same destination or you can change the options of the action and select “Show when run”; if you do that every time you run the workflow it will show you a dialog requesting you to choose the destination folder.

  6. steve hire says:

    -William

    a quick note — I tried again, having changed nothing. I paid close attention to my desktop, and noticed that as the workflow was spinning, a file momentarily appeared on my desktop, then poofed. it was too quick to notice if it was a PDF of my targeted file, but I’m guessing it was. haven’t a clue why it would pop in, then go poof. sort of like quantum matter creation in a void.

  7. steve hire says:

    -William

    Everything you say is as I expected, and it seems to work as expected. My PDFs simply go poof a moment after being created on the desktop.

    I guess the only question is, why should that be? the action works as expected, it’s almost as if my desktop has decided it will destroy all PDFs deposited in it’s space.

    curious — any ideas?

    • William says:

      cupsfilter works in place. It creates a new file on your desktop(popping up of the PDF), if it encounters an error it leaves the erroneous file behind so
      the action deletes(poof) that incomplete file for not leaving unusable files behind.

      I am pretty sure you’re trying to convert a file which cupsfilter cannot handle?

      Note to myself: A better way should be let cupsfilter work with a temporary file and copy the good file to the destination folder, yeah a good improvement, wrote it on my ToDo list.

  8. steve hire says:

    -William

    nope, no error. works perfectly. PDFs just go poof a moment after being created.

    Huh!!!

    /Users/hmshr/Desktop/Picture 1.png

  9. steve hire says:

    should have stated explicitly that I did that directly in Automator, not as a plug-in.

  10. steve hire says:

    -William

    thanks for responding. didn’t really understand, but i think you said there’s nothing i can do.

    will try other files, although the one i tried was a simple .rtf

    will play around some more.

    • William says:

      Right now I’m puzzled… I’ll be glade to here from you if you find out something about that strange behavior.

  11. steve hire says:

    have tried with a simple .rtf, a simple .png and only accomplished a poof on the desktop.

    just for fun, i tried it in terminal running cupsfilter. it too seemed to get to the end, it just did not output a pdf.

    don’t know what to try now. will send a screenshot of terminal effort if you want to see it.

    • William says:

      Yes of course you can, could you also send your system informations? OSX version, cups version? Have you installed other software to handle PDF creation with the printing subsystem?

  12. steve hire says:

    -William

    will comply as best I can

    /Users/hmshr/Desktop/Picture 2.png
    /Users/hmshr/Desktop/Picture 3.png

    these are screenshots (of a sort) of the beginning and end of what I did in terminal. I eliminated the middle, which I think was the actual conversion and just looked like code gibberish.

    I am running 10.5.6, I have not installed any special software to handle pdf creation (than I am aware of, but one never knows). Not sure where to find cups version, but I do a pretty good job of keeping thngs up to date, so I can’t be too far behind in versions, if at all.

    hope you can view these screenshots. if not would be happy to forward otherwise.

  13. William says:

    Sorry I don’t see them. Let’s pursue this error tracking by email, if you like? william@ironicwolf.com

  14. steve hire says:

    will contact via email shortly

  15. William says:

    Many thanks to steve hire for his perseverance! He pointed out a time bomb bug that could have exploded all over the place. If you had any random errors of files converting and some not this was surely caused by a space in the file path. If you experience that problem please download the action again it is now fixed!

  16. Al Maloney says:

    William

    I was having malfunction as well.
    Now your latest version works for me.
    Thank you!

    Is it possible to have the action default to placing the pdf file in the same directory as the original document, with an option to place it elsewhere?

    Al Maloney

  17. Al Maloney says:

    @William
    William

    Thank you for adding my requested change.
    It works – great!

    Al Maloney

  18. Peter Breis says:

    Thank you for Convert to pdf action. I have been looking for something like this for some time.

    My feedback:

    1. Reported the conversion was using an older component, sorry didn’t catch which one.

    2. It errors if you drag a folder full of files into the Specified Finder Items

    3. It was slow

    4. Several of the files were actually larger than the original .eps files, I have never encountered this before.

    5. It uses the Apple pstopdffilter which is again one I have not encountered before

    I have previously used the Unix pstopdf routine, but the most efficient is the one used by Preview which is the Quartz PDFContext using the PSNormalizer.framework.

    The differences in output are:

    Original .eps file: 6.3mb

    Convert to PDF output: 12.6mb

    Preview conversion: 1.8mb

    Illustrator barebones rgb settings: 380kb

    I examined your conversion in Illustrator thinking it maybe was reducing blends to banding which would explain the large file size, but other than a lot of masked blend boxes, I could not see what accounted for the size.

    I looked at the results from Convert to PDF and those from the smallest Illustrator PDF in BBedit and yours seemed to have masses of co-ordinate sets, possibly also color sets, probably for every postscript point, whilst the Illustrator pdf remained much more legible.

  19. Steve Leedy says:

    Can this be used to convert Pages documents to PDFs?

  20. Keith says:

    Is there any way to get this to work when the source file is an rtfd file?

  21. George says:

    does Tiger Server 10.4.11 have the requisite Ruby and CUPS bits installed to support convert-to-pdf? i’ve run successfully on my dev box, a leopard workstation, but am having no luck on Tiger.

    tia, george

  22. [...] – Convert to PDF 1.2 (Action Automator pour convertir des fichiers en PDF) Mise à jour : installateur amélioré, support 32 et 64 bits. GRATUIT • MacOS X 10.5 mini • Site web [...]

  23. I downloaded and tried Convert to PDF, because that is what I needed. But it doesn’t work and I get an error message each time. I ran the sample workflow too. It says Convert to PDF failed = 1 error. It says the action encountered an error – check the action’s properties and try running the workflow again. I don’t do alot of Automator much less scripting. Didn’t know if I was missing something??? Thanks…

  24. There was a blank link at the top of the file:

    /Library/Automator/Convert to PDF.action/Contents/Resources/main.command

    It kept the command from running and caused this error:

    *** NSTask: Task create for path ‘/Library/Automator/Convert to PDF.action/Contents/Resources/main.command’ failed: 8, “Exec format error”. Terminating temporary process.

    Once I removed the blank line at the top of main.command it worked great.


Ironic Wolf is Digg proof thanks to caching by WP Super Cache