pdf2xdp.rb 927 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/env ruby
  2. #
  3. # This script converts a PDF file to an equivalent XML Data Package file,
  4. # which can be opened by Adobe Reader as well and typically escapes AV
  5. # detection better than a "normal" PDF
  6. #
  7. # Alexander 'alech' Klink, 2011
  8. # public domain / CC-0
  9. #
  10. begin
  11. require 'base64'
  12. pdf = ARGV.shift
  13. xdp = ARGV.shift
  14. if ! xdp then
  15. STDERR.puts " Usage: #{$0} input.pdf output.xdp"
  16. exit 1
  17. end
  18. pdf_content = begin
  19. File.read(pdf)
  20. rescue
  21. STDERR.puts "Could not read input PDF file: #{$!}"
  22. exit 2
  23. end
  24. xdp_out = begin
  25. open xdp, 'w'
  26. rescue
  27. STDERR.puts "Could not open output XDP file: #{$!}"
  28. exit 3
  29. end
  30. xdp_out.print '<?xml version="1.0"?><?xfa ?><xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/"><pdf xmlns="http://ns.adobe.com/xdp/pdf/"><document><chunk>'
  31. xdp_out.print Base64.encode64(pdf_content)
  32. xdp_out.print '</chunk></document></pdf></xdp:xdp>'
  33. rescue SignalException => e
  34. puts("Aborted! #{e}")
  35. end