cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

PDF download froM HTTPS URL

MariusStoica
Active Participant
0 Likes
10,506

Hi guys,

I'm trying to download a PDF created by a third party from an API. I'm in a EHP4 FOR SAP ERP 6.0 / NW7.01 environment.

I created using SM50 a HTTP destination (type G and default SSL client) that I use to do POST and GET requests, and everything goes well, unless I try to download a PDF file.

Using:

* Create HTTP client object
    CALL METHOD cl_http_client=>create_by_destination
      EXPORTING
        destination           = 'HTTPS'
      IMPORTING
        client                = lcl_client
      EXCEPTIONS
        destination_not_found = 1
        internal_error        = 2.

I get a correct response:

IF lv_status = 200.
 lv_response = lcl_client->response->get_cdata( ).
ENDIF.

And although "lv_response" has the data that looks correct:

Field    LV_RESPONSE
Data Type          CString{26202}
Absolute Type      \TYPE=STRING

View
      %PDF-1.4##%����##7 0 obj##<</Type /XObject /Subtype /Image /Width 152 /Height 

When I try to transfer the string to the "file" something goes wrong. Here is the code I use to write the file:

DATA: ls_string             TYPE lvc_s_1022,
      lt_string             TYPE lvc_t_1022.

        DO .
          lv_len = STRLEN( lv_response ).
          IF lv_len <> 1022.
            ls_string-line = lv_response+0(lv_len).
          ELSE.
            ls_string-line = lv_response+0(1022).
          ENDIF.
          APPEND ls_string TO lt_string.
          SHIFT lv_response BY 1022 PLACES.
          IF lv_response IS INITIAL.
            EXIT.
          ENDIF.
        ENDDO.

        CONCATENATE lv_file '\' lv_filename '.pdf' INTO lv_file.

        CALL FUNCTION 'SCMS_DOWNLOAD'
          EXPORTING
            filename = lv_file
            binary   = 'X'
            frontend = 'X'
            mimetype = 'charset=UTF8'
          TABLES
            data     = lt_string
          EXCEPTIONS
            error    = 1
            OTHERS   = 2.

        IF sy-subrc <> 0.
          MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                  WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
        ENDIF.

I tried playing with the MIMETYPE parameter and BINARY paratemer, with no success. When opening the file with Acrobat Reader I get the error that the file is corrupt.

Any idea why this doesn't work ?

Kind regards,

Marius

Accepted Solutions (1)

Accepted Solutions (1)

Tomas_Buryanek
Product and Topic Expert
Product and Topic Expert

1) You work with file = binary data. So do not get HTTP response as character data (get_cdata), but i think this metod:

response->get_data

2) You work with binary data so always keep in mind binary data size as raymond.giuseppi suggested.

MariusStoica
Active Participant
0 Likes

Hi Thomas,

Already tried that. It doesn't work 🙂

1. I used

lv_response = lcl_client->response->get_data( ).

to read the response, and used the exact FM with the same parameters and have the same error message in Adobe. As a difference the string looks like :

255044462D312E340D0A25A2A38F930D0A372030206F626A0D0A3C3C2F54797065202F584F626A656374202F53756274797065202F496D616765202F576964746820313532202F486569676874203433202F436F6C6F725370616365202F44657669636547726179202F42697473506572436F6D706F6E656E742038202F446

2. The parameter "binary" is checked in both cases.

Kind regards,
Marius

MariusStoica
Active Participant
0 Likes

Aaaaand,

if I use the type XSTRING for the table

lt_string

to store the data in, I get an short dump.

 Error in ASSIGN statement in program "SAPLSFES".
Tomas_Buryanek
Product and Topic Expert
Product and Topic Expert
0 Likes

Well, I guess you have bugs like using string instead of xstring type and same for data table in SCMS_DOWNLOAD, it should be "binary data" table. For example existing type SOLIX_TAB.

TIP: CL_BCS_CONVERT is very handy class for converting xstring to solix_tab etc...

Tomas_Buryanek
Product and Topic Expert
Product and Topic Expert
0 Likes

Use for example SOLIX_TAB type for SCMS_DOWNLOAD data table.

xstring is not table...

MariusStoica
Active Participant
0 Likes

Tried using this code to transfrom the XSTRING to string .. with no success / errors:

        DATA: xcontent TYPE xstring .
        DATA: content TYPE string .
        DATA: conv   TYPE REF TO cl_abap_conv_in_ce.
        conv = cl_abap_conv_in_ce=>create( input = lv_responsex ).
        conv->read( IMPORTING data = content ).
Tomas_Buryanek
Product and Topic Expert
Product and Topic Expert
0 Likes

Nonono... 😞 You are still going wrong way.

From HTTP client you should have XSTRING. This XSTRING you can convert to SOLIX_TAB (I already gave you a tip how to do it). And this SOLIX_TAB you will use (with file size) in SCMS_DOWNLOAD (or GUI_DOWNLOAD - not sure why you selected to use SCMS_DOWNLOAD ???).

It is quite easy, basic ABAP handling of files...

Answers (1)

Answers (1)

RaymondGiuseppi
Active Contributor

You didn't fill the FILESIZE parameter?

MariusStoica
Active Participant
0 Likes

Hi Giuseppi,

No. I didn't. How would I calculate that ?
Marius.

RaymondGiuseppi
Active Contributor
0 Likes

Try first value of STRLEN( lv_response )

MariusStoica
Active Participant
0 Likes

Hi,

That didn't work. Same error.

I never tried using FM "SCMS_HTTP_GET_FILE" instead of the class. Do you think it might work ?

Or should I use another function to "download" the file ?

Regards,
Marius