on 05-07-2014 1:56 PM
Hi
We want to generate PDF file in SAP backend, and when it is done, we want to send it via SAP Gateway.
A user will push a button, and it will create a report/pdf file with data from backend.
When the pdf file is done, we want to send it back to the user via SAP Gateway.
Anyone who can guide me?
Thanks
Lars
Hi Lars,
In order to consume the PDF, you'll need to make a few GW definitions that are a little out of the ordinary.
Firstly, let's assume you can define the PDF as an entity. To do this you will need some kind of key scheme, e.g. if it were a PDF of an invoice, you can use the invoice ID as the PRF ID as well.
When you define the PDF entity, you need to mark it as 'media' - there is a column for this in the Entity Builder view.
The entity only needs 2 properties - the key and a Mimetype descriptor (string).
You should redefine the MPC_EXT DEFINE method to read like this (note, the names 'PDF' and 'MimeType' depend on the model definition, this is what they are called in my model).
data:
lo_entity type ref to /iwbep/if_mgw_odata_entity_typ,
lo_property type ref to /iwbep/if_mgw_odata_property.
super->define( ).
lo_entity = model->get_entity_type( iv_entity_name = 'PDF' ).
if lo_entity is bound.
lo_property = lo_entity->get_property( iv_property_name = 'MimeType' ).
lo_property->set_as_content_type( ).
endif.
You need to implement the logic for binary extraction in a redefined /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_STREAM method of the DPC.
Here you declare a local variable of type ty_s_media_resource. This has two fields in it, 'mime_type' and 'value'. Place 'application/pdf' in the mime_type and your binary PDF in 'value'. use copy_data_to_ref to copy the local variable to the er_stream output parameter.
Call the URI as normal but add the suffix /$value to access the stream (PDF).
Sorry for brevity but time is short and you can find some longer blogs on the subject of file upload/download which is essentially doing the same thing (When you view a PDF, it is downloading the PDF file to your browser).
Regards
Ron.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Lars,
What Chandra is describing is nothing to do with Gateway
In order to expose a binary representation (such as a PDF or photo) of a resource through OData you need to use the media streaming solution. The client can then use the appropriate handling for the Mime type.
The following code is the tail end of an example of a SAP Office document (which is a PDF) which has been read into table lt_hex - this is converted to binary which is placed in the stream. You need something similar to this in the GET_STREAM logic.
* not very useful code prior to this which fills lt_hex is omitted
data: ls_stream TYPE ty_s_media_resource.
data: lv_solix type solix.
data: lv_input type i.
describe table lt_hex lines lv_input.
read table lt_hex into lv_solix index lv_input.
lv_input = ( ( lv_input - 1 ) * 255 ).
lv_input = lv_input + xstrlen( lv_solix-line ).
call function 'SCMS_BINARY_TO_XSTRING'
exporting
input_length = lv_input
* FIRST_LINE = 0
* LAST_LINE = 0
importing
buffer = ls_stream-value
tables
binary_tab = lt_hex
exceptions
failed = 1
others = 2.
if sy-subrc <> 0.
* Implement suitable error handling here
endif.
ls_stream-mime_type = 'application/pdf'.
copy_data_to_ref( exporting is_data = ls_stream
changing cr_data = er_stream ).
The process I shared was more suitable for my scenario and to display the pdf in front end UI5 application. yes I even can achieve same result using GET_STREAM logic.
and GET_STREAM way is most accurate and recommended one as you need not to generate temporary url using cache logic etc.
And Thanks to for providing more information on it
Regards,
Chandra
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I think it is correct now, but how do I see the pdf document?
Please note, that the backend and Gateway is installed on different systems.
My result now is:
</m:properties>
Thanks,
Lars
Hi,
you can open the PDF file by appending host and port name to your url.
http://<host_name>:<port>/sap/public/005056BC3D0C1EE3B6B4492F419E3CAE.PDF
just type this address in your chrome browser which will show you PDF output.
Regards,
Chandra
Hi Chandra
I don't see anything in my browser, when i type in
http://xxxx.xxx.xx:5xxxx/sap/public/005056BC3D0C1EE3B6B4492F419E3CAE.PDF
Should I choose something else, that I can find in AL11?
you need to type the recent output url. and also note that the generated url output will be seen till 60 seconds as set up in FM logic sapui5-display_smartform_pdf/Z_TEST_PDF_DISPLAY at master · CmIm/sapui5-display_smartform_pdf · GitH...
****Set the Cache Timeout - 60 seconds - we only need this in the cache
****long enough to build the page
lo_cached_response->server_cache_expire_rel( expires_rel = 60 ).
as per your requirement you can change this value.
Now once you execute the odata service, you will get unique url and then just append the host name and open it in the browser.
Regards,
Chandra
My code looks like this:
DATA :
lv_app_type TYPE string,
lv_guid TYPE guid_32,
lo_cached_response TYPE REF TO if_http_response.
data: bindata TYPE XSTRING.
data: xstring type xstring.
data: size1 TYPE abap_msize.
data: string type string.
CALL FUNCTION 'Z_PM_ORDER_PRINT_GET_PDF'
EXPORTING
i_aufnr = i_aufnr
IMPORTING
O_PDF = o_pdf
O_RETURN = o_return
EXCEPTIONS
SOMETHING_WENT_WRONG = 1
OTHERS = 2
.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
string = o_pdf.
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
text = string
* MIMETYPE = ' '
* ENCODING =
IMPORTING
BUFFER = xstring
EXCEPTIONS
FAILED = 1
OTHERS = 2
.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
CREATE OBJECT lo_cached_response
TYPE
cl_http_response
EXPORTING
add_c_msg = 1.
*****set the data and the headers
lo_cached_response->set_data( o_pdf )."lv_pdf_xstring ).
lv_app_type = '.PDF'.
lo_cached_response->set_header_field( name = if_http_header_fields=>content_type
value = lv_app_type ).
*****Set the Response Status
lo_cached_response->set_status( code = 200 reason = 'OK' ).
*****Set the Cache Timeout - 60 seconds - we only need this in the cache
*****long enough to build the page
lo_cached_response->server_cache_expire_rel( expires_rel = 60 ).
*
****Create a unique URL for the object and export URL
CALL FUNCTION 'GUID_CREATE'
IMPORTING
ev_guid_32 = lv_guid.
CONCATENATE '/sap/public' '/' lv_guid '.' 'PDF' INTO e_url.
****Cache the URL
cl_http_server=>server_cache_upload( url = e_url
response = lo_cached_response ).
Hi Ron
It is done now, but I still need something.
I guess it is in method: /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_STREAM
where I have following code:
DATA: ls_media TYPE ty_s_media_resource.
ls_media-mime_type = 'application/pdf' .
ls_media-value = 'x'. "how to replace this with valid data?
copy_data_to_ref(
EXPORTING
is_data = ls_media
CHANGING
cr_data = er_stream
).
When I call
/sap/opu/odata/sap/ZGW_VH_SHOW_PDF_SRV/pdfset(docnum='000040077740')
I got a response with data
When I call
/sap/opu/odata/sap/ZGW_VH_SHOW_PDF_SRV/pdfset(docnum='000040077740')/$value
i got the response 200 OK, and the only text <RESPONSE_BODY />
Can you give me more help?
Thanks
Lars
Thanks, I think it is (nearly) working now.
I call first the URL:
/sap/xxx/xxxx/sap/ZGW_VH_SHOW_PDF_SRV/pdfset(docnum='000040077740')
and after that:
/sap/xxx/xxxx/sap/ZGW_VH_SHOW_PDF_SRV/pdfset(docnum='000040077740')/$value
The only problem now, is that I got the message, that the file is damaged and could not be repaired.
Maybe that is from backend?
User | Count |
---|---|
84 | |
23 | |
11 | |
9 | |
8 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.