Skip to content

Instantly share code, notes, and snippets.

@thomaswitt
Created January 10, 2013 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomaswitt/4503313 to your computer and use it in GitHub Desktop.
Save thomaswitt/4503313 to your computer and use it in GitHub Desktop.
SAP SaaS-Solution Business ByDesign has a Print API, which allows you to spool print jobs to be fetched remotely over the Web. Decoding this API is one of the biggest mess I've ever seen. Anyway, finally I got it right and now I've got a nice ruby printer spooler which you can put in your crontab. It polls the print queue of ByDesign, retrieves …
#!/usr/bin/env ruby
require 'rubygems'
require 'savon'
require 'tempfile'
require 'base64'
USER = 'PRT_001'
PASS = 'YOUR_PASSWORD_GOES_HERE'
SAP_BASE_URL = 'https://my012345.sapbydesign.com'
ENDPOINT = "#{SAP_BASE_URL}/sap/bc/srt/scs/doc/printqueuewebservice3"
OUTPUT_DIR = "/tmp"
# Initialize SOAP Connection to ByDesign
client = Savon.client(:endpoint => ENDPOINT,
:namespace => 'http://sap.com/xi/SAPGlobal20/Global',
:basic_auth => [USER, PASS],
:log => false)
# Retrieve the queue
r = client.call('PrintQueueWebService3GetAllQueueRequest')
queues_info = r.body[:print_queue_web_service3_get_all_queue_response][:queues_info]
id = queues_info[:id]
description = queues_info[:description]
$stderr.puts "*** Found queue #{id} (#{description})"
raise "*** ERROR: No PDF queue" if queues_info[:format_desc] != 'PDF'
uuid = queues_info[:uuid]
# Retrieve next item in the queue
has_next_item = false
begin
r = client.call('PrintQueueWebService3GetNextItemInfoRequest', :message => {
'QueueUUID' => uuid} )
this_item = r.body[:print_queue_web_service3_get_next_item_info_response]
has_next_item = this_item[:has_next_item]
item_uuid = this_item[:item_uuid]
if item_uuid == nil
$stderr.puts "*** No more items to print found"
else
# We actually found one item to print
name = this_item[:document_name]
copies = this_item[:output_copy_number_value]
doc_uuid = this_item[:documents][:document_uuid]
$stderr.puts "*** Found Document #{name || doc_uuid} with #{copies} copies"
if this_item[:documents][:document_type] != 'MAIN' ||
this_item[:documents][:document_mime_type] != 'application/pdf'
raise "*** ERROR: No PDF document"
end
r = client.call('PrintQueueWebService3RetrieveDataRequest', :message => {
'DocumentUUID' => doc_uuid,
'BufferSizeKB' => 0 })
data = r.body[:print_queue_web_service3_retrieve_data_response]
raise "*** ERROR: Could not retrieve document" if data[:end_of_file] != true
pdf_base64 = data[:binary_data]
time = Time.now.strftime('%Y-%m-%d_%H%M%S')
filename = "#{OUTPUT_DIR}/bydesign-#{time}-#{doc_uuid}.pdf"
File.write(filename, Base64.decode64(pdf_base64))
$stderr.puts "*** Sending to print queue"
system("lpr #{filename}")
r = client.call('PrintQueueWebService3SetItemStatusRequest', :message => {
'ItemUUID' => item_uuid,
'ExecutionStatusCode' => 'S',
'ProcessingResultDescription' =>
"Successfully printed on #{time} via ruby" })
end
end while has_next_item
$stderr.puts "*** All done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment