Convert to PDF Version 1.1

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 was astonished not to discover an easy way to...
  2. Flatten Movies Version 1.1 latten Movies is an Automator action to flatten every movie...
  3. Doc to pdf with Automator As requested here is one possible way to convert doc...
  4. Convert to PDF Version 1.2 A long time has passed and our favorite OS has...
  5. Flatten Movies latten Movies is an Automator action to flatten every movie...