Thursday 2 June 2011

SAP SPOOL to PDF file

**&---------------------------------------------------------------------*
**& Report  ZSPOOLTOPDF
**&
**&---------------------------------------------------------------------*
**&
**&
**&---------------------------------------------------------------------*
*
 REPORT  zspooltopdf.
*---Data Declarations
*Tables
 TABLES: tsp01, rststype.
* Internal table
 DATA: it_tsp01  TYPE STANDARD TABLE OF tsp01 WITH HEADER LINE,
       it_table  TYPE STANDARD TABLE OF soli  WITH HEADER LINE,
       it_table1 TYPE STANDARD TABLE OF soli  WITH HEADER LINE,
       it_otf    TYPE STANDARD TABLE OF itcoo WITH HEADER LINE,
       it_doc    LIKE TABLE OF docs,
       it_pdf    LIKE TABLE OF tline,
       it_pdf1   LIKE TABLE OF tline WITH HEADER LINE.
* Work area
 DATA: wa_tsp01 LIKE LINE OF it_tsp01,
       wa_pdf   LIKE LINE OF it_pdf1.
* Varialbles
 DATA: v_string(11) TYPE c VALUE '%',
       v_lines      TYPE i,
       v_size       TYPE i,
       v_client     LIKE tst01-dclient,
       v_name       LIKE tst01-dname,
       v_objtype    LIKE rststype-type,
       v_type       TYPE rststype-type,
       v_otf        TYPE c.
* Constants
 CONSTANTS: c_eof    TYPE soli VALUE '//'.
**&---------------------------------------------------------------------*
 INITIALIZATION.
*---Selection Screen
   SELECT-OPTIONS: s_spool FOR tsp01-rqident.            "Spool id
   PARAMETERS: p_date LIKE sy-datum DEFAULT sy-datum,    "Today's date
               p_dept LIKE tsp01-rqdivision,             "Department
               p_file TYPE string DEFAULT 'C:\temp\test1.pdf'.
*---Start of selection
 START-OF-SELECTION.
   CONCATENATE p_date v_string INTO v_string.
*---get the list of spool nummbers
   SELECT *
     INTO TABLE it_tsp01
     FROM tsp01
     WHERE   rqident    IN   s_spool
       AND   rqdivision EQ   p_dept
       AND   rqcretime  LIKE v_string.
   IF sy-subrc <> 0.
     PERFORM bd_textbox_err(rstxpdft) USING 80
      'Spoolauftrag existiert nicht'(003).
     EXIT.
   ENDIF.
   IF NOT it_tsp01[] IS INITIAL.
     LOOP AT it_tsp01 INTO wa_tsp01.
       CLEAR: v_client, v_name, v_type, v_otf, v_objtype.
       v_client = wa_tsp01-rqclient.
       v_name   = wa_tsp01-rqo1name.
       CALL FUNCTION 'RSTS_GET_ATTRIBUTES'
         EXPORTING
           authority     = 'SP01'
           client        = v_client
           name          = v_name
           part          = 1
         IMPORTING
           type          = v_type
           objtype       = v_objtype
         EXCEPTIONS
           fb_error      = 1
           fb_rsts_other = 2
           no_object     = 3
           no_permission = 4.
       IF v_objtype(3) = 'OTF'.
         v_otf = 'X'.
       ELSE.
         v_otf = space.
       ENDIF.
       IF v_otf = 'X'.
*---convert the spool to an internal table
         REFRESH it_table.
         CALL FUNCTION 'RSPO_RETURN_SPOOLJOB'
           EXPORTING
             rqident              = wa_tsp01-rqident
             first_line           = 1
             last_line            = 0
           TABLES
             buffer               = it_table
           EXCEPTIONS
             no_such_job          = 1
             job_contains_no_data = 2
             selection_empty      = 3
             no_permission        = 4
             can_not_access       = 5
             read_error           = 6
             type_no_match        = 7
             OTHERS               = 8.
         IF sy-subrc <> 0.
           MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
         ENDIF.
         CLEAR v_lines.
         DESCRIBE TABLE it_table LINES v_lines.
         DELETE it_table  INDEX v_lines.
* merge the spools to one big table
         LOOP AT it_table.
           APPEND  it_table TO it_table1.
         ENDLOOP.
         AT LAST.
           APPEND c_eof TO it_table1.
         ENDAT.
       ENDIF.
     ENDLOOP.
     LOOP AT it_table1.
       CLEAR it_otf.
       it_otf = it_table1.
       APPEND  it_otf.
     ENDLOOP.
*---Convert OTF into PDF
     CALL FUNCTION 'CONVERT_OTF_2_PDF'
       EXPORTING
         use_otf_mc_cmd         = 'X'
       IMPORTING
         bin_filesize           = v_size
       TABLES
         otf                    = it_otf
         doctab_archive         = it_doc
         lines                  = it_pdf
       EXCEPTIONS
         err_conv_not_possible  = 1
         err_otf_mc_noendmarker = 2
         OTHERS                 = 3.
     IF sy-subrc <> 0.
       MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                  WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
     ENDIF.
*---Dowload to file
     IF sy-batch NE 'X'.
       CALL METHOD cl_gui_frontend_services=>gui_download
         EXPORTING
           filename = p_file
           filetype = 'BIN'
         CHANGING
           data_tab = it_pdf.
       IF sy-subrc <> 0.
         MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
       ENDIF.

     ELSE.
       DATA: BEGIN OF it_mess_att OCCURS 0,
             i_buffer TYPE string,
       END OF it_mess_att.
       DATA : gd_buffer TYPE string.
       DATA: wa_mess_att LIKE gd_buffer.
       CLEAR it_mess_att.
       REFRESH it_mess_att[].

       LOOP AT it_pdf INTO wa_pdf.
         APPEND wa_pdf TO it_pdf1.
         CLEAR wa_pdf.
       ENDLOOP.
* Transfer the 132-long strings to 255-long strings
       LOOP AT it_pdf1.
         TRANSLATE it_pdf1 USING ' ~'.
         CONCATENATE gd_buffer it_pdf1 INTO gd_buffer.
       ENDLOOP.
       TRANSLATE gd_buffer USING '~ '.
       DO.
         wa_mess_att = gd_buffer.
         APPEND it_mess_att.
         SHIFT gd_buffer LEFT BY 255 PLACES.
         IF gd_buffer IS INITIAL.
           EXIT.
         ENDIF.
       ENDDO.
       OPEN DATASET p_file FOR OUTPUT IN TEXT MODE  ENCODING DEFAULT.
       IF sy-subrc NE 0.
         EXIT.
       ELSE.
*       LOOP AT it_pdf1.
*         TRANSFER it_pdf1 TO p_file.
*       ENDLOOP.
         CLOSE DATASET p_file.
       ENDIF.
     ENDIF.
   ENDIF.

No comments:

Post a Comment