Wednesday, April 25, 2012

SAVE OUTPUT OF A REPORT TO PDF

I hope this code will solve your problem.

The Simple Code to Save the output of a Report to pdf form is :

*&---------------------------------------------------------------------*
*& Report  ZAASHISH_SAVE_TO_PDF
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ZAASHISH_SAVE_TO_PDF NO STANDARD PAGE HEADING LINE-COUNT 40 LINE-SIZE 120.

include ZAASHISH_SAVE_TO_PDF_TOP.
include ZAASHISH_SAVE_TO_PDF_SEL.
include ZAASHISH_SAVE_TO_PDF_SUB.

TOP-OF-PAGE.

PERFORM TOP.

START-OF-SELECTION.

PERFORM PRINT_PARAMETER.

PERFORM FETCH_MARD CHANGING I_MARD.

*PERFORM FETCH_MARC USING I_MARD CHANGING I_MARC.

PERFORM FETCH_MARA USING I_MARD CHANGING I_MARA.

PERFORM FETCH_MAKT USING I_MARD CHANGING I_MAKT.

PERFORM FETCH_FINAL USING I_MARD
                          I_MARC
                          I_MARA
                          I_MAKT
                    CHANGING I_FINAL.

PERFORM DISPLAY_FINAL USING I_FINAL.

PERFORM DISPLAY_PDF.



*&---------------------------------------------------------------------*
*&  Include           ZAASHISH_SAVE_TO_PDF_TOP
*&---------------------------------------------------------------------*

TYPES : BEGIN OF TY_MARA,
        MATNR TYPE MATNR,
        MEINS TYPE MEINS,
        END OF TY_MARA,
      TY_T_MARA TYPE STANDARD TABLE OF TY_MARA,
        BEGIN OF TY_MAKT,
          MATNR TYPE MATNR,
          MAKTX TYPE MAKTX,
        END OF TY_MAKT,
      TY_T_MAKT TYPE STANDARD TABLE OF TY_MAKT,
        BEGIN OF TY_MARC,
          MATNR TYPE MATNR,
          WERKS TYPE WERKS_D,
          PRCTR TYPE PRCTR,
        END OF TY_MARC,
      TY_T_MARC TYPE STANDARD TABLE OF TY_MARC,
        BEGIN OF TY_MARD,
          MATNR TYPE MATNR,
          WERKS TYPE WERKS_D,
          LGORT TYPE LGORT_D,
          LABST TYPE LABST,
          PRCTL TYPE PRCTR,
        END OF TY_MARD,
      TY_T_MARD TYPE STANDARD TABLE OF TY_MARD,
        BEGIN OF TY_FINAL,
          WERKS TYPE WERKS_D,
          MATNR TYPE MATNR,
          MAKTX TYPE MAKTX,
          MEINS TYPE MEINS,
          PRCTL TYPE PRCTR,
          LGORT TYPE LGORT_D,
          LABST TYPE LABST,
        END OF TY_FINAL,
      TY_T_FINAL TYPE STANDARD TABLE OF TY_FINAL.

DATA : I_MARA TYPE TY_T_MARA,
       I_MARC TYPE TY_T_MARC,
       I_MARD TYPE TY_T_MARD,
       I_MAKT TYPE TY_T_MAKT,
       I_FINAL TYPE TY_T_FINAL.

DATA : GV_MATNR TYPE MATNR,
       GV_WERKS TYPE WERKS_D.

DATA:
l_lay TYPE pri_params-paart,
l_lines TYPE pri_params-linct,
l_cols TYPE pri_params-linsz,
l_val TYPE c,
l_no_of_bytes TYPE i,
l_pdf_spoolid LIKE tsp01-rqident,
l_jobname LIKE tbtcjob-jobname,
l_jobcount LIKE tbtcjob-jobcount,
spoolno TYPE tsp01-rqident.
*Types
TYPES:
t_pripar TYPE pri_params,
t_arcpar TYPE arc_params.
"Work areas
DATA:
lw_pripar TYPE t_pripar,
lw_arcpar TYPE t_arcpar.
DATA:
it_t100 TYPE t100 OCCURS 0 WITH HEADER LINE,
it_pdf TYPE tline OCCURS 0 WITH HEADER LINE.


*&---------------------------------------------------------------------*
*&  Include           ZAASHISH_SAVE_TO_PDF_SEL
*&---------------------------------------------------------------------*

SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.

  SELECT-OPTIONS : S_MATNR FOR GV_MATNR OBLIGATORY,
                   S_WERKS FOR GV_WERKS OBLIGATORY.

SELECTION-SCREEN END OF BLOCK B1.



*&---------------------------------------------------------------------*
*&  Include           ZAASHISH_SAVE_TO_PDF_SUB
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&      Form  TOP
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM TOP .
ULINE.
SKIP 2.
WRITE : /50 'MATERIAL NUMBER DESCRIPTION'.
SKIP 3.
WRITE : /5 'MATERIAL NUMBER', 25 'MATERIAL DESC', 70 'UOM', 80 'PRFT CTR',
         100 'STRG LCTN', 110 'STOCK'.
ULINE.
ENDFORM. " TOP
*&---------------------------------------------------------------------*
*&      Form  FETCH_MARD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_I_MARD  text
*----------------------------------------------------------------------*
FORM FETCH_MARD CHANGING FP_I_MARD TYPE TY_T_MARD.

SELECT MATNR
       WERKS
       LGORT
       LABST
       PRCTL
  INTO TABLE FP_I_MARD
  FROM MARD
  WHERE MATNR IN S_MATNR
    AND WERKS IN S_WERKS.

ENDFORM. " FETCH_MARD
*&---------------------------------------------------------------------*
*&      Form  FETCH_MARA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_I_MARD  text
*      <--P_I_MARA  text
*----------------------------------------------------------------------*
FORM FETCH_MARA USING FP_I_MARD TYPE TY_T_MARD
                 CHANGING FP_I_MARA TYPE TY_T_MARA.

IF NOT FP_I_MARD IS INITIAL.
  SELECT MATNR
         MEINS
    INTO TABLE FP_I_MARA
    FROM MARA
    FOR ALL ENTRIES IN FP_I_MARD
    WHERE MATNR = FP_I_MARD-MATNR.
ENDIF.

ENDFORM. " FETCH_MARA
*&---------------------------------------------------------------------*
*&      Form  FETCH_MAKT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_I_MARD  text
*      <--P_I_MAKT  text
*----------------------------------------------------------------------*
FORM FETCH_MAKT USING FP_I_MARD TYPE TY_T_MARD
                 CHANGING FP_I_MAKT TYPE TY_T_MAKT.

  IF NOT FP_I_MARD IS INITIAL.
    SELECT MATNR
           MAKTX
      INTO TABLE FP_I_MAKT
      FROM MAKT
      FOR ALL ENTRIES IN FP_I_MARD
      WHERE MATNR = FP_I_MARD-MATNR.
  ENDIF.
ENDFORM. " FETCH_MAKT
*&---------------------------------------------------------------------*
*&      Form  FETCH_MARC
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_I_MARD  text
*      <--P_I_MARC  text
*----------------------------------------------------------------------*
*FORM FETCH_MARC  USING    FP_I_MARD TYPE TY_T_MARD
*                 CHANGING FP_I_MARC TYPE TY_T_MARC.
*
*IF NOT FP_I_MARD IS INITIAL.
*  SELECT MATNR
*         WERKS
*         PRCTR
*    INTO TABLE FP_I_MARC
*    FROM MARC
*    FOR ALL ENTRIES IN FP_I_MARD
*    WHERE WERKS IN S_WERKS
*      AND MATNR = FP_I_MARD-MATNR.
*ENDIF.
*
*ENDFORM.                    " FETCH_MARC
*&---------------------------------------------------------------------*
*&      Form  FETCH_FINAL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_I_MARD  text
*      -->P_I_MARC  text
*      -->P_I_MARA  text
*      -->P_I_MAKT  text
*      <--P_I_FINAL  text
*----------------------------------------------------------------------*
FORM FETCH_FINAL USING FP_I_MARD TYPE TY_T_MARD
                           FP_I_MARC TYPE TY_T_MARC
                           FP_I_MARA TYPE TY_T_MARA
                           FP_I_MAKT TYPE TY_T_MAKT
                  CHANGING FP_I_FINAL TYPE TY_T_FINAL.
DATA : LW_MARA TYPE TY_MARA,
       LW_MARD TYPE TY_MARD,
       LW_MARC TYPE TY_MARC,
       LW_MAKT TYPE TY_MAKT,
       LW_FINAL TYPE TY_FINAL.

  LOOP AT FP_I_MARD INTO LW_MARD.
    LW_FINAL-MATNR = LW_MARD-MATNR.
    LW_FINAL-WERKS = LW_MARD-WERKS.
    LW_FINAL-LGORT = LW_MARD-LGORT.
    LW_FINAL-LABST = LW_MARD-LABST.
    LW_FINAL-PRCTL = LW_MARD-PRCTL.
*  CLEAR LW_MARC.
*  READ TABLE FP_I_MARC INTO LW_MARC WITH KEY MATNR = LW_MARD-MATNR.
*  IF SY-SUBRC = 0.
*    LW_FINAL-WERKS = LW_MARC-WERKS.
*    LW_FINAL-PRCTR = LW_MARC-PRCTR.
*  ENDIF.
  CLEAR LW_MARA.
  READ TABLE FP_I_MARA INTO LW_MARA WITH KEY MATNR = LW_MARD-MATNR.
  IF SY-SUBRC = 0.
  LW_FINAL-MEINS = LW_MARA-MEINS.
  ENDIF.
  CLEAR LW_MAKT.
  READ TABLE FP_I_MAKT INTO LW_MAKT WITH KEY MATNR = LW_MARD-MATNR.
  IF SY-SUBRC = 0.
    LW_FINAL-MAKTX = LW_MAKT-MAKTX.
  ENDIF.
  APPEND LW_FINAL TO FP_I_FINAL.
  CLEAR LW_FINAL.
  ENDLOOP.
SORT FP_I_FINAL BY WERKS MATNR.

ENDFORM. " FETCH_FINAL
*&---------------------------------------------------------------------*
*&      Form  DISPLAY_FINAL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_I_FINAL  text
*----------------------------------------------------------------------*
FORM DISPLAY_FINAL USING FP_I_FINAL TYPE TY_T_FINAL.

  DATA LW_FINAL TYPE TY_FINAL.
  IF NOT FP_I_FINAL IS INITIAL.
    LOOP AT FP_I_FINAL INTO LW_FINAL.
      AT NEW WERKS.
        WRITE : /5 'PLANT : ', LW_FINAL-WERKS.
        SKIP 2.
      ENDAT.
      WRITE : /5 LW_FINAL-MATNR LEFT-JUSTIFIED , 25 LW_FINAL-MAKTX LEFT-JUSTIFIED ,
             70 LW_FINAL-MEINS LEFT-JUSTIFIED , 80 LW_FINAL-PRCTL, 100 LW_FINAL-LGORT,
             110 LW_FINAL-LABST LEFT-JUSTIFIED.
    ENDLOOP.
  ENDIF.




ENDFORM. " DISPLAY_FINAL

*&---------------------------------------------------------------------*
*&      Form  PRINT_PARAMETER
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM PRINT_PARAMETER .

l_lay = 'X_65_132'.
l_lines = 65.
l_cols = 132.
*Read, determine, change spool print parameters and archive parameters
CALL FUNCTION 'GET_PRINT_PARAMETERS'
EXPORTING
in_archive_parameters = lw_arcpar
in_parameters = lw_pripar
layout = l_lay
line_count = l_lines
line_size = l_cols
no_dialog = 'X'
IMPORTING
out_archive_parameters = lw_arcpar
out_parameters = lw_pripar
valid = l_val
EXCEPTIONS
archive_info_not_found = 1
invalid_print_params = 2
invalid_archive_params = 3
OTHERS = 4.

IF l_val <> space AND sy-subrc = 0.
lw_pripar-prrel = space.
lw_pripar-primm = space.
NEW-PAGE PRINT ON
NEW-SECTION
PARAMETERS lw_pripar
ARCHIVE PARAMETERS lw_arcpar
NO DIALOG.
ENDIF.

ENDFORM. " PRINT_PARAMETER
*&---------------------------------------------------------------------*
*&      Form  DISPLAY_PDF
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM DISPLAY_PDF .

NEW-PAGE PRINT OFF.
CALL FUNCTION 'ABAP4_COMMIT_WORK'.

spoolno = sy-spono.
"Convert spool to PDF
CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
EXPORTING
src_spoolid = spoolno
no_dialog = ' '
IMPORTING
pdf_bytecount = l_no_of_bytes
pdf_spoolid = l_pdf_spoolid
btc_jobname = l_jobname
btc_jobcount = l_jobcount
TABLES
pdf = it_pdf.
"Download PDF file C Drive
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = 'D:/matnr.pdf'
filetype = 'BIN'
TABLES
data_tab = it_pdf.

ENDFORM. " DISPLAY_PDF

No comments:

Post a Comment