Skip to Content
Technical Articles
Author's profile photo Swati Singh

Print DMS attachments in background(Mass Printing)

DMS attachments can be used to provide additional information, for example: Set of instructions for a work order which the personnel needs to carry out or additional details about purchase order. DMS attachments could be in PDF, word, excel format. DMS attachment is printed in foreground by default. These attachments can be saved on local system or shared folders.

In Plant maintenance user wants to print this DMS attachment and provide it to the personnel who is assigned the work order. These documents can be printed for an individual work order via IW32 menu option order->Print Order or Operation Selection. Work orders can also be printed in mass via transaction IW37N by selecting a bunch of work orders->Menu Option Order->Print Order.

During Mass printing from IW37N user expects the DMS attachments of one work order to be printed along that work order. For example if a user has selected 3 work orders to print, the 1st work order should be printed followed by its attachment(s) then the 2nd work order should print followed by its attachment(s) and then the 3rd work order and its attachment(s)

If DMS attachment is of PDF format, the PDF file takes long to get printed. The PDF files also open in foreground in acrobat reader and close only when the document has been printed out. After printing the PDF file closes however the acrobat reader still remains open.

SAP provides function module CVAPI_DOC_VIEW which takes document details as input for printing. This function module however does not provide any parameter to make the printing process synchronous i.e. if there are multiple attachments move to the 2nd attachment only when 1st one has completed printing or move to printing the the next work order only when all attachments of the preceding work order have been printed.

CALL FUNCTION 'CVAPI_DOC_VIEW'
   EXPORTING
      pf_dokar      = <Doc Type>
      pf_doknr      = <Doc Number>
      pf_dokvr      = <Doc Version>
      pf_doktl      = <Doc Part>
      pf_apptp      = '3'          " app type print = 3
      pf_parent     = blank
   IMPORTING
      pfx_file      = lv_filename    "File path
   EXCEPTIONS
      error         = 1
      not_found     = 2
      no_auth       = 3
      no_original   = 4
      OTHERS        = 5.

IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

This can be resolved by using the method cl_gui_frontend_services=>execute to print, by passing the synchronous parameter as X. This however creates another problem. Lets understand this. We read above that PDF files open in acrobat reader and even after the file has been printed the acrobat application still remains open. since we are printing in mass and printing synchronously to maintain the sequence of work order followed by its attachments the program remains hung until the user manually closes the acrobat application.

CALL METHOD cl_gui_frontend_services=>execute
   EXPORTING
      document       = <File path with filename to print>
      synchronous    = 'X'   "until user closes acrobat application further processing does not happen
      operation      = 'PRINT'
   EXCEPTIONS
      cntl_error             = 1
      error_no_gui           = 2
      bad_parameter          = 3
      file_not_found         = 4
      path_not_found         = 5
      file_extension_unknown = 6
      error_execute_failed   = 7
      synchronous_failed     = 8
      not_supported_by_gui   = 9
      OTHERS                 = 10.

IF sy-subrc NE 0.
* Implement suitable error handling here
ENDIF.

User is definitely not going to want to do so for every PDF attachment while printing in mass!

To resolve this we decided on sending the DMS attachment(PDF) data to spool. This can be achieved by using the ADS(Adobe Document Services) function modules provided by SAP.

Here goes the sample code. I am sure this will help a lot of us as I searched a lot and could not find solution that wasn’t suggesting to use a third party system.

DATA: it_pdf          TYPE STANDARD TABLE OF tline,
      lt_lines        TYPE STANDARD TABLE OF tline,
      lt_binary       TYPE STANDARD TABLE OF raw255,
      ls_pdf          TYPE tline,
      ls_binary       TYPE raw255,
      lv_content      TYPE xstring,
      lv_length       TYPE sy-tabix,
      lv_raw          TYPE rsrd_rawstring,
      lv_spool_id     TYPE rspoid,
      lv_partname     TYPE adspart,
      lv_fname_spool  TYPE char256,
      lv_dest         TYPE rspopname,
      lv_spool_handle TYPE sytabix.

FIELD-SYMBOLS <fs_x> TYPE x.

*upload the file: could be local system or shared folders
*lv_filename contains the file path with name . this can be found in DRAW table

CALL FUNCTION 'GUI_UPLOAD'
   EXPORTING
      filename   = lv_filename
      filetype   = 'BIN'
   IMPORTING
      filelength = lv_length
   TABLES
      data_tab   = it_pdf.

*concatenate all data into one string
LOOP AT it_pdf INTO ls_pdf.
   ASSIGN ls_pdf TO <fs_x> CASTING.
   CONCATENATE lv_content <fs_x> INTO lv_content IN BYTE MODE.
ENDLOOP.

*convert form xstring to binary
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
   EXPORTING
      buffer        = lv_content
   IMPORTING
      output_length = lv_length
   TABLES
      binary_tab    = lt_binary.

LOOP AT lt_binary INTO ls_binary.
   ASSIGN ls_binary TO <fs_x> CASTING.
   CONCATENATE lv_raw <fs_x> INTO lv_raw IN BYTE MODE.
ENDLOOP.

*Open Spool
CALL FUNCTION 'ADS_SR_OPEN'
   EXPORTING
      dest             = 'LCPD' " // your output device
      doctype          = 'ADSP'
      immediate_print  = 'X'
      auto_delete      = 'X'
   IMPORTING
      handle           = lv_spool_handle
      spoolid          = lv_spool_id
   EXCEPTIONS
      device_missing   = 1
      no_such_device   = 2
      operation_failed = 3
      wrong_doctype    = 4
      wrong_devicetype = 5
      OTHERS           = 6.

IF sy-subrc NE 0.

ENDIF.

*Reads the content of a part file of an ADS spool
CALL FUNCTION 'ADS_GET_PARTFILE_NAME'
   EXPORTING
      rqident    = lv_spool_id
      partnumber = 1
   IMPORTING
      partname   = lv_partname.

CONCATENATE lv_partname '.pdf' INTO lv_fname_spool.

*write binary content to spool
CALL FUNCTION 'ADS_WRITE_TO_FILE'
   EXPORTING
      filename                     = lv_fname_spool
      buffer                       = lv_raw
   EXCEPTIONS
      cannot_open_file             = 1
      open_dataset_no_authority    = 2
      open_dataset_internal_error  = 3
      open_dataset_too_many_files  = 4
      dataset_cant_close           = 5
      close_dataset_internal_error = 6
      cannot_close_file            = 7
      cannot_transfer_data         = 8
      transfer_internal_error      = 9
      dataset_write_error          = 10
      OTHERS                       = 11.

*Write a line in an open ADS spool request
CALL FUNCTION 'ADS_SR_CONFIRM'
   EXPORTING
      handle           = lv_spool_handle
      partname         = lv_partname
      size             = lv_length
      pages            = 1
   IMPORTING
      new_partname     = lv_partname
   EXCEPTIONS
      handle_not_valid = 1
      operation_failed = 2
      OTHERS           = 3.

*close the spool request
CALL FUNCTION 'ADS_SR_CLOSE'
   EXPORTING
      handle           = lv_spool_handle
   EXCEPTIONS
      handle_not_valid = 1
      operation_failed = 2
      OTHERS           = 3.

WAIT UP TO 10 SECONDS.

This way we can convert PDF data to spool without opening the acrobat reader. The wait statement allows us to maintain the sequence by waiting for a few seconds to let the PDF document finish printing. the method cl_gui_frontend_services=>execute can still be used to print word/excel files synchronously as these files do not take long to print. The whole code can be called in a loop for printing multiple attachments of same work order.

Assigned Tags

      19 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sammar Razdhan
      Sammar Razdhan

      Nice Utility!! Keep it up!!

      Author's profile photo Swati Singh
      Swati Singh
      Blog Post Author

      Thanks Sammar!

      I struggled a lot with this issue and was only able to solve this after a long time so thought of extending my experience and knowledge on this to others 🙂

       

       

      Author's profile photo Bertrand BOULEUC
      Bertrand BOULEUC

      hello Swati,

      I have same issue on Production side : it is printing in mass production order and DMS documents, Users are complaining that shop floor paper and pdf link through DMS are printing very slowly since several year and we never succed to speed it up.

      I will show your document to the development team, maybe we ca ngo in this direction.

      If you know a way to print other extension file like dwg (autocad) ou tif, this would help !

       

      Thanks

      Bertrand

      Author's profile photo Swati Singh
      Swati Singh
      Blog Post Author

      Hi Bertrand,

       

      Have you tried converting these docs to pdf pdf format?

       

      Thanks,

      Swati

      Author's profile photo Bertrand BOULEUC
      Bertrand BOULEUC

      Hello Swati,

      documents are all in pdf format.

      We are able to print going through Windows function printing of pdf (no SAP spool created).

      It is working but it is very slow.

      I didn't request dev team to use your proposal since we are overloaded for dev...

      Anyway, i keep it as a solution for future.

      We have another plant where we have multiple document format like picture format and autocad and pdf and they want to print all documents related to one sale order in mass...

      Tell me if you think it is feasible ?

      Best regards

      Bertrand

      Author's profile photo Bhanu Prakash Maddi
      Bhanu Prakash Maddi

      Hello Swati,

       

      this was very helpful to print pdf documents to the spool.

      I am looking to print the docx, jpeg and xls to the spool.

      is this feasible. Can you share your insights pls.

       

      Thank you.

      Bhanu

      Author's profile photo Hilary Dennis
      Hilary Dennis

      I cannot thank you enough - this is just awesome !!!

      i created a function module and loop over a number of pdfs and i append them all into one spool file

      this is just great !!!

       

      Am just wondering if I can append these pdf's to smartform output spool file 

      Author's profile photo Swati Singh
      Swati Singh
      Blog Post Author

      So glad that it helped you!

      are you looking at downloading the spool data at a later stage?

      Author's profile photo Rajesh Ramayan Koothrappali
      Rajesh Ramayan Koothrappali

      Thank you so much Swati, it works very good to print DMS in production orders.

      Keep it up!

      Author's profile photo Swati Singh
      Swati Singh

      Thanks Rajesh. Glad it helped you

      Author's profile photo KARAN KADAM
      KARAN KADAM

      Hi Rajesh . R Can you share the Document How to Print DMS in Production order

      One of Our client Requirement is to get Document created automatically without using CV01n the Production order Spool Print should get automatically attached for Production order Document section.

      Author's profile photo Ashok Gadde
      Ashok Gadde

      Thank You so much Swati...it helped me in understanding the DMS document to print.

      spool get generated but not able to view it.

      Author's profile photo Swati Singh
      Swati Singh

      Hi Ashok,

      I am passing parameter to delete spool immediately. If you make that blank, you should be able to see.

      Author's profile photo Ashok Gadde
      Ashok Gadde

      Hi Swati,

      i have passed a blank for auto delete but not able to view the spool and getting info message as file does not begin with '%pdf  . When printed to physical printer..it is printed as junk values.

      i am trying to print word and text.

      Author's profile photo Swati Singh
      Swati Singh
      Blog Post Author

      Hi Ashok,

      This solution is to print PDF documents. word will not work. you will have to convert to PDF before printing.

       

      Thanks,

      Swati

      Author's profile photo Ajeeth ram
      Ajeeth ram

      Hi Swati,

      Thank you very very much for this blog..

      I was searching for a way to mass print DMS documents stored in content server in front end and you have given me 95 % of the solution in a single blog..

      Once again Thank you.. 🙂

      Ajeeth Ram

      Author's profile photo Glenn Devleeschouwer
      Glenn Devleeschouwer

      Hi Ajeeth,

      I have done the same; get multiple PDF files and combine them in 1 PDF and print immediate. The spool file looks ok, but when we print this raw data is printed out. Have you had the same issue and if so, how did you solve? Thanks! Glenn

      Author's profile photo Ajeeth ram
      Ajeeth ram

      Hi Glenn,

       

      Initially i faced this problem, but updating the printer driver solved mine. Nothing to do with SAP.

       

      Regards,

      Ajeeth

      Author's profile photo Sabariayyappan Balasubramaniyan
      Sabariayyappan Balasubramaniyan

      Hi Swathi

       

      Is this will work in Fiori based application like Manage notifications and orders information center/manage maintenance order list ?

       

      Thanks

      Sabari