Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Convert XML to PDF Directly

0 Kudos

Hello Experts,

I want to accept XML file from user and convert it directly into PDF and let it downloaded to local system.

Is there any way I can do it.

I have tried below code but PDF file is getting corrupt...

CALL METHOD gs_cl_xml->import_from_file
    EXPORTING
      filename = p_file 
    RECEIVING
      retcode = gv_subrc.

IF gv_subrc = 0.
  CALL METHOD gs_cl_xml->render_2_xstring
      IMPORTING
        retcode = gv_subrc
        stream = gv_xml_string
        size = gv_size.

  IF gv_subrc = 0.
    CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
        EXPORTING
           buffer = gv_xml_string
        TABLES
           binary_tab = lt_data.
    cl_gui_frontend_services=>gui_download(
      exporting
        filename = 'C:\Users\ast07367\Desktop\PDF.pdf'
        filetype = 'BIN'
      changing
        data_tab = LT_DATA ).
  Endif.
Endif.

3 REPLIES 3

Tomas_Buryanek
Active Contributor
0 Kudos

SCMS_XSTRING_TO_BINARY function has exporting parameter OUTPUT_LENGTH, which is important value which you should use in GUI_DOWNLOAD. If you do not use it, 'BIN' gui_download does not know the correct size of the created file.

But anyway, I am not sure if you can simply save binary XML and call it PDF. But that is another thing.

-- Tomas --

matt
Active Contributor

In future please use the "code" button on the editor to post code examples. Use right click "post as text" when pasting code from an editor, and after posting, ensure the code is nicely formatted and readable.

I've done it for you this time.

DoanManhQuynh
Active Contributor
0 Kudos

You cant directly convert from XML to PDF, they are difference thing. You can convert data from XML to internal table, write it to spool then export spool to PDF. See this wiki ( you can replace WRITE statement with fm: RSPO_WRITE_SPOOLREQUEST in order to write data to spool ) .

https://wiki.scn.sap.com/wiki/display/XI/Generation+of+PDF+File+from+Internal+Table%2C+SAP+Script%2C...

plus, see this report to know more about spool to pdf: RSTXPDFT4.