SAP Document Management (DMS) is one of the central functions in Logistics and cross-application component of SAP ECC. It supports many document management functions for SAP system as well as external systems. Please refer SAP help portal and SCN wiki for more details.
Several times there is requirement to print documents from DMS. One such scenario is printing Work Packets from Maintenance Order (IW32) through menu Order -> Print -> Order (or Operation selection). But the below discussion is not restricted to this scenario and generally applicable for all printing requirements of DMS documents.
These documents may have varying formats – PDF, Word, Excel, PPT, JPEG, etc. They may be stored on some third party system like SharePoint. Sending them to spool for printing for all these formats is not feasible. Here comes handy the standard SAP class supporting many front-end functions CL_GUI_FRONTEND_SERVICES.
If the documents are stored on an external system, you need to get the document as string of Hexadecimal characters (it may be proxy call to PI system, which was the scenario in my case). You can get the document into an internal table in binary format for subsequent processing:
DATA: xbuffer TYPE xstring, filelength TYPE i, it_data TYPE TABLE OF tbl1024. CALL FUNCTION ‘SCMS_XSTRING_TO_BINARY‘ EXPORTING buffer = xbuffer IMPORTING output_length = filelength TABLES binary_tab = it_data.
If the document is stored locally, you may perhaps use F/M SDOK_PHIO_LOAD_CONTENT. I have not worked on this scenario though. Once you have the document in the internal table, you can download to the local machine (Concatenate the file name, you desire, to the file_name field below to make it a complete filename with path):
DATA: filelength TYPE i, file_name TYPE string VALUE ‘C:\Temp’, file_len TYPE i. CALL METHOD cl_gui_frontend_services=>gui_download EXPORTING bin_filesize = filelength filename = file_name filetype = ‘BIN’ IMPORTING filelength = file_len CHANGING data_tab = it_data EXCEPTIONS file_write_error = 1 OTHERS = 2.
Just one remark about C:\temp, it would be better to use the SAP GUI temporary directory or the workdir (in 2 steps: cl_gui_frontend_services=>get_temp_directory + cl_gui_cfw=>flush for transferring the value to the variable ; note that this directory is emptied from time to time by the SAP GUI).
The bin_filesize parameter above is optional and seems to be innocuous but it’s not. I missed to pass the file length to this parameter (I got from F/M SCMS_XSTRING_TO_BINARY) and to my annoyance I was unable to open the downloaded file (with message: The file is damaged) whatever I did. Somebody with good experience with binary files came to my rescue and suggested to pass this parameter and it worked after that. Lesson of the story is file length (in bytes) is very important when dealing with binary files.
Upon successful download, you can send it to printer by calling the class’s EXECUTE method:
CALL METHOD cl_gui_frontend_services=>execute EXPORTING document = file_name operation = ‘PRINT’ EXCEPTIONS …..
After printing the document successfully, you may want to delete it which can be handled by FILE_DELETE method of the class:
CALL METHOD cl_gui_frontend_services=>file_delete EXPORTING filename = file_name CHANGING rc = rc EXCEPTIONS file_delete_failed = 1 OTHERS = 9. IF rc = 32. MESSAGE i461(iw) WITH ‘File is open in another window. Please close doc ‘. ENDIF.
So that’s how this class handles all aspects of front-end handling of file automating download, print and delete functions. You may print several documents of different formats keeping the above logic in a LOOP. Enjoy !