Convert to PDF Version 1.1

2009
05.08
PDF

PDF

A minor update for a developer a major improvement for a lot of users. I would like to thank for all the feedbacks I’ve got, for Version 1. It had quite a few problems; important enough to be immediately addressed.

What changed

  • A more clean, robust source code.
  • Added a requested feature: to be able to choose the source folder(of the input file) as the destination folder.
  • Fixed a path error by properly escaping the file string.
  • It works now in the temporary folder, so no more files flashing around.

Download Version 1.1

No need for additional software on Mac, if the software we already have is maintained, to succeed in performing virtuously tasks.

Download Convert To PDF

Ruby Dopamine

Here is still the Automator Ruby code that accomplish the painful task of batch processing files to PDF. It got a little bit longer, but can handle bug tracking better that way. It was a mess to figure out what kind of string; escaped or not, cupsfilter would expect.

require 'tmpdir'
require 'fileutils'

ARGF.each do |f|

  #
  # Input file
  #
  input_file = f.chop # get rid of the newline charachter
  input_file_extension = File.extname(input_file)
  input_file_basename = File.basename(input_file, input_file_extension)
  input_file_name = "#{input_file_basename}#{input_file_extension}"
  input_folder = File.dirname(input_file)

  #
  # Final output folder
  #
  if ENV['same_folder'] == '1' then
    output_folder = input_folder
  else
    output_folder = File.expand_path((o = ENV['output']).nil? ? "~/Desktop" : o)
  end

  #
  # Final output file
  #
	output_file_name = "#{input_file_basename}.pdf"
	output_file = File.join(output_folder, output_file_name)

  #
  # Temporary input/output file
  #
  # cupsfilter doesn't handle well filenames with latin-1 encoding.
  # tried to escape the filename but doesn't help cupsfilter
  # require to work on a copy with ASCII encoding.
  input_copy = File.join(Dir.tmpdir,'original.cupsfile')
  temporary_file = File.join(Dir.tmpdir,'converted.cupsfile') 

  #
  # Execute cupsfilter
  #
  FileUtils.copy(input_file, input_copy)
  `cupsfilter "#{input_copy}" 2> /dev/console > "#{temporary_file}"`
  FileUtils.remove_file(input_copy) if File.exists?(input_copy)

  #
  # Check if cupsfilter converted successfully
  #
  if $?.success? then
    FileUtils.mv(temporary_file, output_file)
    puts output_file # return the new file to automator
  else
    FileUtils.remove_file(temporary_file) if File.exists?(temporary_file)
    $stderr.puts "Cannot convert #{input_file_name}"
    exit 1
  end
end
Related posts:
  1. Convert to PDF I was astonished not to discover an easy way to...
  2. Convert to PDF Version 1.2 A long time has passed and our favorite OS has...
  3. Flatten Movies Version 1.1 Flatten Movies is an Automator action to flatten every movie...
  4. Doc to pdf with Automator As requested here is one possible way to convert doc...
  5. Flatten Movies Flatten Movies is an Automator action to flatten every movie...

Tags: , , , , , , ,

12 Responses to “Convert to PDF Version 1.1”

  1. agnt_smith says:

    isn’t it possible to convert .doc? will it ever be possible?
    further more: i want to send the converted files via email, so the pdf needn’t be saved anywhere. is there an option?

    thanks for the great script!

    • William says:

      I address the question “convert to doc” in a new post Doc to pdf as it might be useful for everyone and as I googled around it’s a topic of its own.

      For the second point I have to investigate the capabilities of Automator’s variables, a little bit and the possibility of storing the intermediated files in a temporary directory.
      As I understand the Automator philosophy is: get files, modify files, create files, modify files etc and than trash the files you don’t want? If an action creates temporary files some where, you aren’t aware of, who will be responsible/supposed to delete those files? It gets messy. I think it’s a design choice to let the user see what’s happening when the workflow runs.

  2. Tom Arnfeld says:

    Hello,
    I love this..
    One thing i would suggest.. instead of making it a workflow, make it a bit more friendly by making it an Application.

    Like some of these…

    What would help to make it an application is not to let uses add the files, but use the “Ask for Finder Items” and make sure the “Allow Multiple Selections” box is ticked… then just add a Growl notification.. some confirmation messages and your off…

    Although you would not get the option of where to save it..

    Good Luck!

  3. Brian says:

    I have tried installing and using this script. I see the script in my Automator, but cannot for the life of me figure out how I can use it. I cannot figure out where to drag and drop my .doc or .xls file so that the conversion begins even??

    HELP!

    Thanks.

    • Bernat says:

      Install the action and then create a service using Automator. It’s very easy.
      Select documents in Finder and then add the action ‘Convert to PDF’.
      After saving the service, go to your file, click right and select ‘Convert to PDF’

      Enjoy!

  4. Bernat says:

    About your Automator action ‘Convert to PDF’

    Hi,

    I’m happy because finally I found an Automator action to transform files to PDF. Otherwise, the action has a ‘bug’… For example, if I try to convert an horizontal PS file, the result is a vertical PDF without all the information. The action cuts the page.

    If you have the intention to solve this problem, let me know “bernat.puigdomenenech@gmail.com”

    Thanks for sharing,
    Bernat

  5. Ronny J says:

    Thank you for the script! Works great too. I do have one issue/question though. I convert .tiff to .pdf just fine. But it’s rotating the pdf. is there a way to change that? I don’t know code very well yet at all.
    Any advice or help would be wonderful.

  6. Le Blanc B. Jefferson says:

    This might be a good idea if the developer had provided decent directions on how to use it. As it is, it appears to have been designed by a geek for geeks. I’m not enough of a geek to figure it out. I suggest the author go back to the drawing board – or find someone who knows how to use it to write a decent set of instructions. I think the suggestion above to make it an application has merit.

    • Tripp H says:

      Mr. Jefferson,

      In polite company where I come from, we would say that you are looking the gift horse in the mouth. If you are not geek enough for this, then perhaps you might try getting some education instead of whining about work that other people have done for free for your benefit.

  7. Tripp H says:

    I too am getting an error message. I have tested the files and they do print via the Finder File menu Print command. I used your sample workflow and gave it the file that previously printed via the Finder. Any ideas on what could be off?

  8. bm1967 says:

    I tried this automator but each time I run it, I get an error. Looking at the code you are using (cupsfilter, which is really a converter linked to it), I was a bit disappointed. What I am looking for is the ability to access Apple’s native ‘Print’ command that would enable me to save a file (any file) to PDF. The major difference between CUPs convert and Apple’s own ‘Print’ utility is the quality and active hyper links. Using cupsfilter, HTML pages come chopped up and not properly formatted. Too bad. I have a ton of HTML docs to convert so I guess, I will be still looking.

    I was originally doing everything on the command line so when I saw your automator post, I was excited, for a second until I saw how you do it in the background….

    • William says:

      Hi bm1967,

      thank you for you comment. I didn’t try to hide the fact that Convert to PDF is only a front-end to cupsfilter ;) It is meant for people not at ease with shell script or command line, its an attempt to offer cupsfilter( sadly with its limitations) to automator folks. Did you try with cupsfilter on command-line? Could you please send me a sample file, which produces an error? Up until now I have no clue what people are trying to convert and what causes problems.

      I investigated hard and deep, the sad conclusion is that without hacking through undocumented Apple API, there is no problematically way of calling Apple’s native save to pdf.

      If you have a lot of docs of the same kind and you have the time and will to spend, I know of one sure path to proceed:
      I would try to automate the Application, which shows your document the best, in such way, to print through a PDF printer.
      F.ex Word can be automated with VBscript, nearly all mac applications support Applescript and a Cups-pdf printer can be found at codepoetry dot net: Cups-pdf-for-macosx

      From dat point on, create a simple Automator frontend to print through the PDF printer.


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