Showing posts with label Exchange of PDF idoc's. Show all posts
Showing posts with label Exchange of PDF idoc's. Show all posts

Tuesday, September 2, 2014

Payslips PDF files to IDoc and IDoc to PDF file conversion program - Base 64 format.

Algorithm for the PDF data transmission:
Outbound:
-          Read the PDF file into an internal table ( via program ) – Binary form
-          Convert Binary table data into Xstring  
-          Encode Xsting Data into Base64 format
-          Create an Idoc  with the Base 64 format
Inbound:
-          Receive/read the idoc
-          Decode Base64 data into Xstring
-          Xstring into to Binary
-          Binary file store in directory

-          Create pdf file from the directory

       Very simple and sample Code attached :  

       *&---------------------------------------------------------------------*
*& Report  ZTESTPDF_IDOC
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

   REPORT  ZTESTPDF_IDOC.

** Created sample for for POC **
** By Giridhar Pujari **
** Please dont delete **

    tables: Z1FDATA,Z1FLINE,EDID4.

    SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE text-001.
    PARAMETERS: p_fpath LIKE rlgrap-filename,
              P_B64  as checkbox,
              P_IDOC AS checkbox default 'X'.


SELECTION-SCREEN END OF BLOCK blk1.

Selection-screen begin of block blk2 with frame title Text-002.

Parameters:   P_D64  as checkbox,
              P_IDOC1 like Edid4-DOCNUM.
*            p_file2 LIKE rlgrap-filename.

Selection-screen end of block blk2.

Data: p_file LIKE rlgrap-filename.


**********************************************

   DATA: i_idoc_data    LIKE edidd OCCURS 0 WITH HEADER LINE.
   DATA: i_idoc_control LIKE edidc OCCURS 0 WITH HEADER LINE.
   DATA: control_record LIKE edidc.

   data: i_communication like edidc occurs 0 with header line.

   data: begin of l_bitmap occurs 0,
        l(1000) type x,
      end of l_bitmap.

    data: begin of base64 occurs 0,
      Sdata(1000) Type C,
      end of base64.

data: l_filename    type string,
      l_bytecount   type i,
      l_type_in(4)  type c,
      l_type_out(4) type c.
data: l_itflines    type standard table of tline,
      l_textheader  type thead.


DATA: lv_filename TYPE string.
DATA: lv_path     TYPE string.
DATA: lv_fullpath TYPE string.
DATA: lv_content  TYPE xstring.
DATA: lv_length   TYPE i.
DATA: lv_rc TYPE sy-subrc.

data: sigdata          type XSTRING.
data: sigdata_b64      type STRING.
data: sigdata_test     type STRING.

DATA: i_ZFLINE Like z1fline,
      i_Z1FDATA like Z1FDATA.
Data: i_ZPDDATA like ZPDDATA.



data: header_segment_name like edidd-segnam value 'ZFLINE',
      item_segment_name like edidd-segnam value 'Z1FDATA'.

data: len Type i.

Data: Flag type C.

*&---------------------------------------------------------------------*
*&                   AT SELECTION SCREEN EVENT
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fpath.

  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      program_name  = syst-cprog
      dynpro_number = syst-dynnr
      field_name    = ' '
    IMPORTING
      file_name     = p_fpath.


Start-of-selection.

If not p_fpath is initial.

l_filename = p_fpath.

call function 'GUI_UPLOAD'
    exporting
      filename                      = l_filename
      filetype                      = 'BIN'
    importing
      filelength                    = l_bytecount
    tables
      data_tab                      = l_bitmap.

IF Sy-subrc eq 0.

CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
  EXPORTING
    INPUT_LENGTH       = l_bytecount
*   FIRST_LINE         = 0
*   LAST_LINE          = 0
 IMPORTING
   BUFFER             = lv_content
  TABLES
    BINARY_TAB         = l_bitmap
* EXCEPTIONS
*   FAILED             = 1
*   OTHERS             = 2
          .
IF SY-SUBRC eq 0.

Write:/ lv_content.

* Implement suitable error handling here

CALL FUNCTION 'SSFC_BASE64_ENCODE'
  EXPORTING
    BINDATA                        = lv_content
*   BINLENG                        =
 IMPORTING
   B64DATA                        =  sigdata_b64
* EXCEPTIONS
*   SSF_KRN_ERROR                  = 1
*   SSF_KRN_NOOP                   = 2
*   SSF_KRN_NOMEMORY               = 3
*   SSF_KRN_OPINV                  = 4
*   SSF_KRN_INPUT_DATA_ERROR       = 5
*   SSF_KRN_INVALID_PAR            = 6
*   SSF_KRN_INVALID_PARLEN         = 7
*   OTHERS                         = 8
          .
IF SY-SUBRC EQ 0.
* Implement suitable error handling here

New-page.
Write 'BASE64 Data'.
write Sigdata_b64.

ENDIF.


* Sending an IDoc.
CALL FUNCTION 'SCMS_STRING_TO_FTEXT'
  EXPORTING
    TEXT            = sigdata_b64
* IMPORTING
*   LENGTH          =
  TABLES
    FTEXT_TAB       = base64
          .
 Len = strlen( sigdata_b64 ).

*
*CALL FUNCTION 'CONVERT_STRING_TO_TABLE'
*  EXPORTING
*    I_STRING               = sigdata_b64
*    I_TABLINE_LENGTH       = Len
*  TABLES
*    ET_TABLE               = l_bitmap
*          .


*If Sy-subrc eq 0.

control_record-mestyp = 'YGLPDF'.
control_record-rcvprt = 'LS'.
control_record-idoctp = 'YPDF_FILE'.
control_record-rcvprn = 'PX8'.


Loop at base64.


If Flag is initial.

Move 'ZPHEADER' to i_idoc_data-segnam.
Move 'BEPPPAYDATEEMPNO' to i_idoc_data-Sdata.
MOVE sy-mandt TO i_idoc_data-mandt.
Append i_idoc_data.

flag = 'X'.

Endif.


i_ZPDDATA-ZDAta = base64-sdata.
MOVE i_ZPDDATA TO i_idoc_data-sdata.
MOVE 'Z1FDATA' TO i_idoc_data-segnam.
MOVE sy-mandt TO i_idoc_data-mandt.

Append i_idoc_data.
endloop.

.


*endif.

CALL FUNCTION 'MASTER_IDOC_DISTRIBUTE'
  EXPORTING
    MASTER_IDOC_CONTROL                  = control_record
*   OBJ_TYPE                             = ''
*   CHNUM                                = ''
  TABLES
    COMMUNICATION_IDOC_CONTROL           = i_communication
    MASTER_IDOC_DATA                     = i_idoc_data
* EXCEPTIONS
*   ERROR_IN_IDOC_CONTROL                = 1
*   ERROR_WRITING_IDOC_STATUS            = 2
*   ERROR_IN_IDOC_DATA                   = 3
*   SENDING_LOGICAL_SYSTEM_UNKNOWN       = 4
*   OTHERS                               = 5
          .
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.

Endif.

Endif.
endif.


*** Decoding functionality ****

IF not P_D64 is initial.

*Clear lv_content.
*Clear
**refresh l_bitmap.


data: begin of PDFDATA occurs 0,
      SDATA like edid4-sdata,
      end of pdfdata.

data: SDATA type string.



clear base64.

*Sdata = Sigdata_b64.

*pdfdata = Sigdata_B64.
*

   *sigdata_test = pdfdata.


   Data: lv_dtint2 type EDI_DTINT2.

*
   Select SDATA into table PDFDATA
       from EDID4
       where DOCNUM = P_IDOC1.

   
   If sy-subrc eq 0.

    loop at PDFDATA.

   *concatenate PDFDATA-SDATA into sigdata_b64.

   CONCATENATE LINES OF PDFDATA INTO sigdata_b64 RESPECTING BLANKS.

   endloop.


   CALL FUNCTION 'SSFC_BASE64_DECODE'
   EXPORTING
    B64DATA                        = sigdata_b64
*   B64LENG                        =
*   B_CHECK                        =
 IMPORTING
   BINDATA                         = lv_content
* EXCEPTIONS
*   SSF_KRN_ERROR                  = 1
*   SSF_KRN_NOOP                   = 2
*   SSF_KRN_NOMEMORY               = 3
*   SSF_KRN_OPINV                  = 4
*   SSF_KRN_INPUT_DATA_ERROR       = 5
*   SSF_KRN_INVALID_PAR            = 6
*   SSF_KRN_INVALID_PARLEN         = 7
*   OTHERS                         = 8
          .
   IF SY-SUBRC Eq 0.
* Implement suitable error handling here


  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
    BUFFER                = lv_content
*   APPEND_TO_TABLE       = ' '
* IMPORTING
*   OUTPUT_LENGTH         =  l_bytecount
  TABLES
    BINARY_TAB            = l_bitmap
          .
   If Sy-subrc eq 0.

**writing file to AL11 and recreating file again with PDF.

    P_File = '/local/data/interface/AE.PDF'.

   OPEN DATASET P_file FOR OUTPUT IN Binary MODE.
  
   IF Sy-subrc eq 0.
   LOOP AT l_bitmap.
     TRANSFER l_bitmap TO p_file.
    ENDLOOP.
     CLOSE DATASET p_file.
   endif.


 CALL FUNCTION 'ARCHIVFILE_SERVER_TO_CLIENT'
   EXPORTING
     PATH             = '/local/data/interface/AE.PDF'
   TARGETPATH       = 'C:\Users\G.Pujari\Desktop\BE_Giri1.pdf'
   EXCEPTIONS
   ERROR_FILE       = 1
   OTHERS           = 2
          .
   IF SY-SUBRC <> 0.
*     Implement suitable error handling here
   ENDIF.


   endif.


   ENDIF.


*
   endif.

   endif.