Skip to Content
Author's profile photo Chandrashekhar Mahajan

Display Smartform (PDF) in SAPUI5

UPDATE

Please see the last section Improved and Recommended approach to Display PDF.

This approach is based on SAP GW Media streaming solution and is very easy to implement.

Please change you coding/design accordingly.


Introduction –


Many times I saw questions on requirement to display smartform in SAPUI5.

Below are few threads on same requirement,

How to call smart from in sapui5?

Print Forms in SAP UI5

Open PDF retrieved via odata service in SAPUI5

Adobe form and SAPUI5

Showing pdf in sapui5 using a string.


Hence I thought to write blog on this subject and put focus on step-by-step procedure to display smartform in SAPUI5. I assume that many of us will be interested to know how to get the smartform (pdf) content on SAPUI5 by exposing the backend data in the form of Gateway (OData) service.


Let me explain all these details by taking simple example of standard smartform SF_EXAMPLE_03 (Smart Forms Training Example 3). Our objective will be to display the output of smartform SF_EXAMPLE_03 in SAPUI5 application in the form of PDF. We will have customer number as the input field and will get the flight details invoice as an PDF output. We will use iframe as container to display the PDF.



Procedure –

Very first, we will develop function module Z_TEST_PDF_DISPLAY which will take customer  number as input and will export URL pointing to PDF. You can get the logic of function module at sapui5-display_smartform_pdf/Z_TEST_PDF_DISPLAY at master · CmIm/sapui5-display_smartform_pdf · GitHub

P.S. Inside FM, I used the logic from thread Mime logo not displaying to build temporary URL for PDF.

Now we will develop GW service which will help us to expose this data. Let’s create simple GW service ZTESTPDF as displayed below.
/wp-content/uploads/2014/02/ui5_sf1_378313.jpg

We will have entity as pdf and entity set as pdfset. generate runtime artifacts and then go to generated class ZCL_ZTESTPDF_DPC_EXT. Now Redefine method PDFSET_GET_ENTITY and put below code. You can also get below code at sapui5-display_smartform_pdf/PDFSET_GET_ENTITY at master · CmIm/sapui5-display_smartform_pdf · GitHub



DATA:
   lt_keys     TYPE /iwbep/t_mgw_tech_pairs,
   ls_key      TYPE /iwbep/s_mgw_tech_pair,
   lv_customer TYPE s_customer,
   lv_url      TYPE string.
   lt_keys = io_tech_request_context->get_keys( ).
   READ TABLE lt_keys WITH KEY name = 'CUSTOMER' INTO ls_key.
   lv_customer = ls_key-value.
   CALL FUNCTION 'Z_TEST_PDF_DISPLAY'
     EXPORTING
       i_customer = lv_customer
     IMPORTING
       e_url      = lv_url.
   er_entity-customer = lv_customer.
   er_entity-url = lv_url.






Now register your GW service and That’s it! We are now ready to test our GW service. Just query with customer number and you should be able to get the output as highlighted below.

/wp-content/uploads/2014/02/ui5_sf2_378333.jpg


At this point, we are done with backend logic and GW service development. Now we will develop SAPUI5 application which will consume this service.


Create SAPUI5 project and put code in index.html.(follow MVC pattern as best practice!). You will get source code of index.html at sapui5-display_smartform_pdf/index.html at master · CmIm/sapui5-display_smartform_pdf · GitHub


End Result –


Now deploy your application to SAP ABAP server and run the application. provide any customer number (check if data is available in table SCUSTOM) and click on Display PDF button. You will see below result on screen. Here, we are displaying invoice details of customer number 3 in the form of smartform pdf.


/wp-content/uploads/2014/02/ui5_sf3_378424.jpg


Closing Remarks –


With the simple technique of creating temporary URL for the PDF and then exposing that URL through GW service, we are building SAPUI5 application to consume GW service. We are then setting src property of an iframe with this URL and setting this content to HTML UI element.


I hope with this simple way, you can display any backend smartform in SAPUI5 application !


I will request you to put the comment on this blog. Please feel free to put your comments/suggestions and any other way to display smartform in sapui5!


Happy Learning and Coding 🙂 🙂 🙂


Improved and Recommended approach to Display PDF –

[Added on 30th June 2014]


Using OData Media indicator, we can easily download any files in SAP Gateway. This is very well explained in this blog How To Upload and Download Files Using SAP NW Gateway SP06 by Jan Thomas Nygaard


I will use similar approach for our current functionality and below are the steps involved into it. You need to mark the entity type pdf as Media enabled as displayed in below snap. also Need to take property MimeType of type string. This will be used to set the mime type of the file to be downloaded. You can refer Supported MIME Types –  ABAP Workbench Tools – SAP Library for more information.


/wp-content/uploads/2014/02/ui5_sf4_487035.jpg

Now we need to redefine DEFINE Method of class ZCL_ZTESTPDF_MPC_EXT as below,

METHOD define.

  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.

ENDMETHOD.

GET_STREAM method of class ZCL_ZTESTPDF_DPC_EXT as below,

METHOD /iwbep/if_mgw_appl_srv_runtime~get_stream.

  DATA:

  lt_keys     TYPE /iwbep/t_mgw_tech_pairs,

  ls_key      TYPE /iwbep/s_mgw_tech_pair,

  lv_customer TYPE s_customer,

  lv_xstring  TYPE xstring,

  ls_stream   TYPE ty_s_media_resource.

  lt_keys = io_tech_request_context->get_keys( ).

  READ TABLE lt_keys WITH KEY name = ‘CUSTOMER’ INTO ls_key.

  lv_customer = ls_key-value.

  CALL FUNCTION ‘Z_TEST_PDF_DISPLAY’

    EXPORTING

      i_customer = lv_customer

    IMPORTING

      e_xstring = lv_xstring.

  ls_stream-value = lv_xstring.

  ls_stream-mime_type = ‘application/pdf’.

  copy_data_to_ref( EXPORTING is_data = ls_stream

                    CHANGING  cr_data = er_stream ).

ENDMETHOD.

As SAP Gateway performs changing the XSTRING content to proper media format, we need not to write extra logic of generating url etc as explained in FM logic sapui5-display_smartform_pdf/Z_TEST_PDF_DISPLAY at master · CmIm/sapui5-display_smartform_pdf · GitHub

Instead of that, we just need to export the XSTRING content from FM. Improved version of FM logic is as below,

FUNCTION z_test_pdf_display.

*”———————————————————————-

*”*”Local Interface:

*”  IMPORTING

*”     REFERENCE(I_CUSTOMER) TYPE  S_CUSTOMER

*”  EXPORTING

*”     REFERENCE(E_XSTRING) TYPE  XSTRING

*”———————————————————————-

* Data Declaration

  DATA :

  lv_fm_name             TYPE rs38l_fnam,

  ls_output_options      TYPE ssfcompop,

  lv_language            TYPE tdspras,

  ls_control_parameters  TYPE ssfctrlop,

  ls_output_data         TYPE ssfcrescl,

  lv_pdf_len             TYPE i,

  lv_pdf_xstring         TYPE xstring,

  lt_lines               TYPE TABLE OF tline,

  lv_devtype             TYPE rspoptype,

  lv_app_type            TYPE string,

  lv_guid                TYPE guid_32,

  lo_cached_response     TYPE REF TO if_http_response,

  ls_customer            TYPE  scustom,

  lt_bookings            TYPE  ty_bookings,

  lt_connections         TYPE  ty_connections,

  lt_tstotf              TYPE tsfotf.

* language

  lv_language = sy-langu.

  TRANSLATE lv_language TO UPPER CASE.

  ls_control_parameters-langu = lv_language.

* set control parameters to get the output text format (OTF) from Smart Forms

  ls_control_parameters-no_dialog = ‘X’.

  ls_control_parameters-getotf   = ‘X’.

  ls_control_parameters-preview = space. “No preview

* get device type from language

  CALL FUNCTION ‘SSF_GET_DEVICE_TYPE’

    EXPORTING

      i_language            = lv_language

*     i_application          = ‘SAPDEFAULT’

    IMPORTING

      e_devtype             = lv_devtype

    EXCEPTIONS

      no_language           = 1

      language_not_installed = 2

      no_devtype_found      = 3

      system_error          = 4

      OTHERS                = 5.

* set device type in output options

  ls_output_options-tdprinter = lv_devtype.

* Set relevant output options

  ls_output_options-tdnewid = ‘X’. “Print parameters,

  ls_output_options-tddelete = space. “Print parameters

  CALL FUNCTION ‘SSF_FUNCTION_MODULE_NAME’

    EXPORTING

      formname          = ‘SF_EXAMPLE_03’  “Smartform name

    IMPORTING

      fm_name           = lv_fm_name

    EXCEPTIONS

      no_form           = 1

      no_function_module = 2

      OTHERS            = 3.

* Data retrieval and supplying it to Samrtform FM

  SELECT SINGLE * FROM scustom INTO ls_customer WHERE id = i_customer.

  SELECT * FROM sbook INTO TABLE lt_bookings   WHERE customid = i_customer.

  SELECT * FROM spfli INTO TABLE lt_connections UP TO 10 ROWS.

* Call Smartform generated FM

  CALL FUNCTION lv_fm_name

    EXPORTING

      control_parameters = ls_control_parameters

      output_options    = ls_output_options

      user_settings     = space

      customer          = ls_customer

      bookings          = lt_bookings

      connections       = lt_connections

    IMPORTING

      job_output_info   = ls_output_data

    EXCEPTIONS

      formatting_error  = 1

      internal_error    = 2

      send_error        = 3

      user_canceled     = 4

      OTHERS            = 5.

  APPEND LINES OF ls_output_data-otfdata[] TO lt_tstotf[].

* Convert to OTF

  CALL FUNCTION ‘CONVERT_OTF’

    EXPORTING

      format               = ‘PDF’

    IMPORTING

      bin_filesize         = lv_pdf_len

      bin_file             = lv_pdf_xstring       ” binary file

    TABLES

      otf                  = lt_tstotf

      lines                = lt_lines

    EXCEPTIONS

      err_max_linewidth    = 1

      err_format           = 2

      err_conv_not_possible = 3

      err_bad_otf          = 4

      OTHERS               = 5.

  IF sy-subrc = 0.

    e_xstring = lv_pdf_xstring.

  ENDIF.

ENDFUNCTION.

To download the file in GW Client, we need to execute query as /sap/opu/odata/sap/ZTESTPDF_SRV/pdfset(‘5’)/$value and hence accordingly below are the changes need to be made in UI5 application,


var oButton = new sap.ui.commons.Button({
       id : 'B-Create',
       text : 'Display PDF',
       width : '10em',
       press : function(){
       var sRead = "/pdfset(customer='" + oTF.getValue() + "')" + "/$value" ;
   
       oModel.read( sRead, null, null, true, function(oData, oResponse){
                     var pdfURL = oResponse.requestUri;            
            html.setContent("<iframe src=" + pdfURL + " width='700' height='700'></iframe>");
            oPanel.addContent(html);
            oPanel.placeAt("content");
        
                   
        },function(){
            alert("Read failed");});
          
       }
       });


That’s it.

Please use this approach of media streaming solution. This is better and easy to implement.

Assigned Tags

      89 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Great tutorial, hopefully it will assist people with this very question in the future 🙂 .

      Thanks for sharing.

      Kind regards

      Miki

      Author's profile photo Former Member
      Former Member

      Thanks for this information, we currently use the same approach and I will be looking at moving this to a Custom Control over the next few weeks, I write a blog about this as well and share the code.

      Martin

      Author's profile photo Chandrashekhar Mahajan
      Chandrashekhar Mahajan
      Blog Post Author

      Sure! That will gr8 for all of us...waiting for your blog 🙂

      Author's profile photo Krishna Kishor Kammaje
      Krishna Kishor Kammaje

      Nice one Chandra. But I see one problem. Whenever UI5 application is in Gateway server and smartform pdf is pointing to the backend server, then we might have to make use of reverse proxy to overcome SOP. May be a better option is to get the pdf as an XSTRING. what do you say?

      Author's profile photo Chandrashekhar Mahajan
      Chandrashekhar Mahajan
      Blog Post Author

      Yes Krishna, there will be SOP issues for the scenario you mentioned. Regarding xstring, I can also export xstring from my GW service but I did not found any simple way to convert xstring to PDF on client side i.e UI5 application.may be in that case we may have to use some 3rd party libraries etc.Please let us know in case you know any better way to convert xstring to pdf on client side.

      Regards,

      Chandra

      Author's profile photo Former Member
      Former Member

      Hmm, when SAP backend can convert xstring to PDF, why you cannot execute same FM on Gateway and provide link to generated pdf on gateway?

      Author's profile photo Syambabu Allu
      Syambabu Allu

      Hi Chandra,

      Good Work....Is it possible to call Interactive forms through gateway?

      Thanks,

      Syam

      Author's profile photo Chandrashekhar Mahajan
      Chandrashekhar Mahajan
      Blog Post Author

      Hi Syam,

      in this blog, I just shared process to display smartform PDF and I guess with the similar way, we can display interactive form output but not sure if further interaction with the form will be possible in ui5 context. as of now I do not have much experience with interactive form hence cannot commit more on it.

      Regards,

      Chandra

      Author's profile photo Syambabu Allu
      Syambabu Allu

      Hi Chandra,

      Thanks for info..Have you found any 3rd party lib files for XSTRING to PDF on client side i.e UI5.

      Thanks,

      Syam

      Author's profile photo Chandrashekhar Mahajan
      Chandrashekhar Mahajan
      Blog Post Author

      Hi Syam,

      As of now I did not found any 3rd party lib to convert XSTRING to PDF on client side. I will surely update this blog in case I get any good solution.

      Regards,

      Chandra

      Author's profile photo Syambabu Allu
      Syambabu Allu

      Hi Chandra,

      Thanks.. 🙂

      Author's profile photo Uday Kumar Kanike
      Uday Kumar Kanike

      Nice blog, very helpful. Thanks a lot and please do share other SAPUI5 examples.

      Thanks

      Uday

      Author's profile photo Former Member
      Former Member

      Hi Chandar,

      First of all i would like thank you for your wonderful blog.

      I have created in FM, Service in NW GW and UI Screen at SAP UI5 with respect to your blog.

      I got below response for my service.

      pdf url.PNG

      pdf.PNG

      CONCATENATE  '/sap/public' '/' lv_guid '.' 'PDF' INTO e_url.

      Function module is returning url like

      URL =  /sap/public/46C64CAC93CB1ED3B2BF04D15B23D801.PDF

      Capture pdf.PNG

      I have consumed above service or url in my ui5 application.

      Capture pdf2.PNG

      I got below error while running UI5 application.

      Capture pdf3.PNG

      Can you help on this? Where i done the mistake?

      Do i need to pass PDF converted string to UI5 as odata?

      Thanks in advance.

      Akshath

      Author's profile photo Chandrashekhar Mahajan
      Chandrashekhar Mahajan
      Blog Post Author

      This error related to CORS . Instead of running it on localhost, you can deploy it on ABAP AS and then run on it. else you need to setup proxy as mentioned in blog How to create SAPUI5 application consuming Gateway service with the help of SAP NW Gateway Plug-in for Eclipse

      Regards,

      Chandra

      Author's profile photo Former Member
      Former Member

      Hello Chandrashekhar Mahajan,

      I had tried the above code, and its working perfectly fine on desktop,but when I am using the same code on mobile device(android Tablet). It just displays blank page.

      Help is appreciated

      Thanks and Regards

      Roshan Jalgaonkar

      Author's profile photo Chandrashekhar Mahajan
      Chandrashekhar Mahajan
      Blog Post Author

      Hi,

      Have you tried it with get_stream approach which I mentioned in this blog?

      Regards,

      Chandra

      Author's profile photo Former Member
      Former Member

      Hi ,

      I have used following code to display pdf in sapui5 application

      var docURL=undefined;

      var docType=undefined;

      var temp;

      var html=new sap.ui.core.HTML();

      oModel.read(readUrl, undefined, undefined, false, function(oData, oResponse){  

             docURL= oResponse.requestUri;

             temp=oResponse.headers;

             docType=temp["Content-Type"];

      },function(oError){

             errorHandling(oError); 

      }); 

      if(docType=="application/pdf" || docType=="image/jpeg"){

             html.setContent("<iframe src='" + docURL + "' type='"+docType+"' width='600' height='700'> </iframe>");

      }else{

             html.setContent("<embed src='" + docURL + "' title='attachment'" +

             " width='1000px' height='1000px'></embed>");

      }

      Help is appreciated

      Thanks and Regards

      Roshan Jalgaonkar

      Author's profile photo Former Member
      Former Member

      I'm currently facing the same issue, in Desktop it works perfectly, but in the tablet (iPad) it simply doesn't work, it shows only the pdf without scrolling bar. This seems to be a common issue with iPad (Safari) and I've already tried so many different approaches to replace the IFRAME, without success.

      Author's profile photo Hüseyin Erbek
      Hüseyin Erbek

      Hi Jeferson did you solve  the issue ? I am facing the same issue too.

      Author's profile photo Michael Appleby
      Michael Appleby

      Please create a new Discussion marked as a Question.  The Comments section of a Blog (or Document) is not the right vehicle for asking questions as the results are not easily searchable.  Once your issue is solved, a Discussion with the solution (and marked with Correct Answer) makes the results visible to others experiencing a similar problem.  If a blog or document is related, put in a link.  Read the Getting Started documents (link at the top right) including the Rules of Engagement. 

      NOTE: Getting the link is easy enough for both the author and Blog.  Simply MouseOver the item, Right Click, and select Copy Shortcut.  Paste it into your Discussion.  You can also click on the url after pasting.  Click on the A to expand the options and select T (on the right) to Auto-Title the url.

      Thanks, Mike (Moderator)

      SAP Technology RIG

      Author's profile photo Former Member
      Former Member

      Hi, I've created an iframe within the view and manually resize it, it was the only way to fix the issue. Below the code snippet of only the part that matters for you.

      function resize_pdf() {

        var height = window.innerHeight; //Firefox

        var headerHeight = 51;

        if (document.body.clientHeight) {

        height=document.body.clientHeight; //IE

        }

        //resize the iframe according to the size of the

        //window (all these should be on the same line)

        document.getElementById("pdfId").style.height = parseInt(height - document.getElementById("pdfId").offsetTop - headerHeight)+"px";

      }

      sap.ui.core.mvc.Controller.extend("PDF.view.Attachment", {

        onInit: function() {

        var htmlId = this.byId("htmlId");

        htmlId.setContent( "<iframe id=\"pdfId\" src=\"\" frameborder=\"0\" width=\"100%\" />" ); 

        sap.ui.core.UIComponent.getRouterFor(this).attachRoutePatternMatched(this.handleRoutePatternMatched, this);

        },

        handleRoutePatternMatched: function(oEvent) {

        if (oEvent.getParameter("name") === "Attachment") {

        // Every time user resizes the browser window, adjust Attachment size

        window.onresize = resize_pdf;

        }

        },

        onAfterRendering: function() {

        // First time loading Attachment view (iframe wouldn't be visible from RouteMatched yet)

        $("#pdfId").attr("src", this._sServiceUrl);

        resize_pdf();

        }

      });

      Author's profile photo Former Member
      Former Member

      Hi

      Thanks for sharing such a useful document, keep posting more such docs..

      Cheers 😉

      Pradyp

      Author's profile photo Former Member
      Former Member

      Hello Chandrashekhar ,

      I am getting a status code 500 "internal server error". The message that I am getting is

      "Method 'PDFSET_GET_ENTITY' not implemented in data provider class."

      Can you please help me with the same. I am new to ABAP so your help would be appreciated.

      Author's profile photo Syambabu Allu
      Syambabu Allu

      Hi Mohit,

      you have redefine the PDFSET_GET_ENTITY and implement the code same in this method.

      Thanks,

      Syam

      Author's profile photo Former Member
      Former Member

      Hi Syam ,

      Thank You for your reply. But I am still getting an  error. I was activating it and it says the "ZCL_YTESTPDF_DPC_EXT , method PDFSET_GET_ENTITY" has syntax errors and the error is at line 22 which is

      " er_entity-url = lv_url." . I have used the same code, but I don't know why this happening .

      Author's profile photo Syambabu Allu
      Syambabu Allu

      Hi Mohit,

      What is the error getting?

      Thanks,

      Syam

      Author's profile photo Former Member
      Former Member

      Hi Syam ,

      It just says syntax error and it points me row 22  which is " er_entity-url = lv_url."

      Also there is another error which says The data object "ER_ENtity" does not have a component called URL.

      Author's profile photo Syambabu Allu
      Syambabu Allu

      Hello Mohit,

      In your entity type have the URL property?..if it not have you have to create one property with name URL.

      Thanks,

      Syam

      Author's profile photo Former Member
      Former Member

      Hi Syam ,

      Thanks for your reply. I did create the property with name URL , but it still gives me the same error. Below are the screenshots of the same.

      /wp-content/uploads/2014/07/error2_498123.png

      /wp-content/uploads/2014/07/error1_498122.png

      Author's profile photo Chandrashekhar Mahajan
      Chandrashekhar Mahajan
      Blog Post Author

      Hi Mohit,

      Kindly create discussion thread for your issue.

      Regards,

      Chandra

      Author's profile photo Vince Tebano
      Vince Tebano

      When I go edit METHOD /iwbep/if_mgw_appl_srv_runtime~get_stream.  I get the error System setting does not allow changes to be made to the object CLAS /IWBEP/CL_MGW_ABS_DATA.  If I can /IWBEP to be modifiable the system then asks me a developers key to modify standard SAP code.

      This does not seem right?  Is there a step missing?

      Author's profile photo Chandrashekhar Mahajan
      Chandrashekhar Mahajan
      Blog Post Author

      Hi,

      you need to redefine the method in your Z*_DPC_EXT class.

      Regards,

      Chandra

      Author's profile photo Vince Tebano
      Vince Tebano

      Thanks,  I got the redefines in and was testing the read but now I am getting:

      Method 'PDFSET_GET_ENTITY' not implemented in data provider class.  For the stream approach I didn't think we had to modify this.  Do you know why it is not getting generated?

      Author's profile photo Former Member
      Former Member

      Hi Vince,

      I was getting the same error before.

      Just make sure of two thing that you have redefined the method PDFSET_GET_ENTITY correctly and after making changes you are generating the run time objects .

      Author's profile photo Vince Tebano
      Vince Tebano

      The two things that I saw that were to be redefined were :

           ZCL_ZTESTPDF_MPC_EXT -> DEFINE

            ZCL_ZTESTPDF_DPC_EXT -> GET_STREAM


      The PDFSET_GET_ENTITY was described as only to be used in the URL version of the code.  If you redefine the get_entity code what goes in there because it does not show any changes to it.  If you leave it as is and remove the URL parameter to add the MimeType you will get an error stating the URL parameter is not there.  Also,  In that get_entity I would not think you would want to call the FM z_test_pdf_display again since it getting called in the get_stream. 

      Author's profile photo Chandrashekhar Mahajan
      Chandrashekhar Mahajan
      Blog Post Author

      Hello Vince Tebano and Mohit Sachdeva

      for the 2nd approach, you need not to redefine PDFSET_GET_ENTITY. As mentioned, you just need to redefine method DEFINE and GET_STREAM . Also please note that FM z_test_pdf_display is exporting only E_XSTRING as we do not require URL parameter to be exported.

      and to test the functionality, you need to execute URI as below,


      /sap/opu/odata/sap/ZTESTPDF_SRV/pdfset('5')/$value

      $value will trigger GET_ENTITY method.

      I hope this clears all the doubts 🙂

      Regards,

      Chandra

      Author's profile photo Vince Tebano
      Vince Tebano

      Yes it does. 

      I was testing the link:

           /sap/opu/odata/sap/ZTESTPDF_SRV/pdfset('5')

      Which was calling the Read PDFSET_GET_ENTITY (which was not defined).

      After changing the link to:

           /sap/opu/odata/sap/ZTESTPDF_SRV/pdfset('5')/$value

      It is hitting the GET_STREAM that was redefined. 

      Makes perfect sense now.

      Thanks for help,

      Vinnie

      Author's profile photo Pawan Kalyan
      Pawan Kalyan

      Hi Chandrashekhar,

      Document is very good.. But i am able to see my PDF.

      I am not getting any error but i am not able to see PDF Content , I am seeing only IFRAME.

      For Iframe SRC , i have used XSTRING and uploaded it in server cache just like you mentioned in the above tutorial.

      Error.png

      Why my PDF Content is not getting Displayed. Please tell me .. I am in serious need of some assistance.

      Author's profile photo Yen Shen Lim
      Yen Shen Lim

      Hi @Pawan Kalyan

      I'm having the same issue as yours, do you get the solutions?

      Regards,

      Ben

      Author's profile photo Former Member
      Former Member

      Hi Chandrashekhar,

      This is a very informative blog, thank you very much for sharing this.

      Though I am working on a very different requirement where I am generating Salary Slips of employees and displaying them in PDF format in a UI5 application. I am fetching details of Salary for the employee using a standard RFC on the SAP end. This RFC returns the required data in XString format.

      Now my requirement is to convert this XString into PDF format. I have two alternative with me:

      1. I pass this XString as is through the OData service and handle it in my UI5 application and convert this into a PDF format and display the same. The issue is that I have not been able to figure out a solution on how to handle XString in the UI5 app and convert it into PDF format.
      2. The other option is that I convert this into PDF format within my wrapper RFC on the SAP end and store this PDF into cache memory, then generate a URL for this file, pass it to the UI5 application and call this URL from the app to display the PDF in an iFrame. Here I am facing serious issues with the conversion of XString into PDF format and the generated PDF is in a very distorted format, hence not acceptable.

      Request you to please help me with your inputs on this critical issue so I can find a feasible solution on the same. Please let me know in case there is any further information you would require from me to help you understand my issue better.

      Thanks,

      Abhishek Goel.

      Author's profile photo Chandrashekhar Mahajan
      Chandrashekhar Mahajan
      Blog Post Author

      Hi Abhishek,

      for 2nd option, Use GET_STREAM method approach as explained in the blog. Check with that and let me know if you still face any issue.

      Regards,

      Chandra

      Author's profile photo Former Member
      Former Member

      I really appreciate your prompt response.

      I have tried following as your blog has directed, but I think the GET_STREAM method only discusses conversion of a SmartForm into PDF and sending that PDF through gateway to the UI5 application. Please, kindly correct me if I am wrong in my understanding.

      But in my scenario I do not have any SmartForm at all and just getting a single XString as output from that RFC. So I am not sure how to convert that XString into PDF and send it through the service.

      Thanks,

      Abhishek.

      Author's profile photo Arshad Ansary
      Arshad Ansary

      Hi Chandrasekhar,

      Excellent blog and nice way to return back the data in  xstring  manner via  get_stream method.

      How can we use the same $value link in URL for GET_ENTITYSET case..

      In the below URL , it is not calling the GET_STREAM method

      /sap/opu/odata/sap/ZGW_ARS_EMP_SRV/EmployeeSet?$filter eq EmployeeID eq '000004'/$value

      If this is supported then the URL can have filter,sorted,$select values also..

      One more doubt is what relevance does the below have in MPC

        IF lo_entity IS BOUND.

          lo_property = lo_entity->get_property( iv_property_name = 'MimeType' ).

          lo_property->set_as_content_type( ).

        ENDIF.


      You are no where using this property for stream value assignment

      Regards

      Arshad

      Author's profile photo Dhananjay Choubey
      Dhananjay Choubey

      Hi Chandrashekhar,

      It's a nice document and it helped me alot. I just followed your steps and everything was working well. I am doing it in localhost.

      I got this error.

      pdf_error.JPG

      I have also opened a sdn discussion.

      Simple example for gateway and odata communication 

      Thanks in advance

      Dhananjay

      Author's profile photo Former Member
      Former Member

      Hello Chandrashekhar,


      Thank you for this helpful document.

      I followed the steps you described  in the improved and recommended approach and everything was working just fine until the last step when I could not see the pdf document.

      It displays only the frame, but no content.

      I tested the service using the REST Client and I get the pdf, so I don't think that the service is the problem.

      I must mention that I created an Android application, so I was wondering if there are any modifications that must be performed on the source code of the SAPUI5 application in order to see the PDF on Android.

      Also, I want to mention that I am using the service through Gateway and not locally.

      Thanks in advance,

      Stefan

      /wp-content/uploads/2014/11/2014_11_18_16_07_03_588042.png

      Author's profile photo Dhananjay Choubey
      Dhananjay Choubey

      once check by increasing your cache time.

      ****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 ).
      Author's profile photo Karthik Arjun
      Karthik Arjun

      Hi Stefan,

      Ref This : http://scn.sap.com/community/developer-center/front-end/blog/2015/01/23/disable-https-security-certificate-in-mobiletablet--android-and-ios

      1. Download PDF from Service to Local File by using window.preference concepts

      2. then use file opener plugin to open downloaded file from your local device

      Hope this will work.

      Thanks,

      Karthik A

      Author's profile photo Karthik Arjun
      Karthik Arjun

      Hi,

      I am facing some problem to display pdf in Android application.

      Here we used iframe, object tags. But no use 🙁

      Can anyone please give your idea to view pdf in android application.

      Note: Same code(iframe tag)  working fine in desktop. but in mobile/tablet it is not working.

      (I am using SAP Gateway service)

      Thanks,

      Karthik A

      Author's profile photo Former Member
      Former Member

      Hi Karthik,

      Did you able to solve the problem Kindly Help me .

      Author's profile photo Karthik Arjun
      Karthik Arjun

      Hi karthik,

      I solved this issue...

      Ref This : http://scn.sap.com/community/developer-center/front-end/blog/2015/01/23/disable-https-security-certificate-in-mobiletablet--android-and-ios

      1. Download PDF from Service to Local File by using window.preference concepts

      2. then use file opener plugin to open downloaded file from your local device

      Follow this link...

      It is working...

      Thanks,

      Karthik A

      Author's profile photo Ravi kiran R
      Ravi kiran R

      Hi Thank you for the excellent and very useful article

      Can we use this for excel file download also

      In the function module  if i convert the excel to xstring

      and in the gateway if i change the   ls_stream-mime_type = 'application/pdf' to application/msexcel

      Will this work ?

      Thanks a lot

      Author's profile photo Chandrashekhar Mahajan
      Chandrashekhar Mahajan
      Blog Post Author

      yes you can download excel file as well. check this blog Displaying backend data both as SAPUI5 Table and an Excel File but stick to improved approach of using media steaming.

      Regards,

      Chandra

      Author's profile photo Ravi kiran R
      Ravi kiran R

      Thank you Mahajan, we will be trying it today.

      Regards,

      Ravikiran

      Author's profile photo T Fdo
      T Fdo

      Thank you very much Chandrashekhar.. Very Nice and Informative...

      Author's profile photo Former Member
      Former Member

      Hi Chandrashekhar,

      I have referred your blog as I am also having same kind of requirement, but facing an issue. Could you please check the below posted thread and give your valuable input on that.

      open the smartform as a PDF in UI5

      Regards,

      Jaten Sangal

      Author's profile photo Vishnu Pankajakshan Panicker
      Vishnu Pankajakshan Panicker

      Hi Chandra,

      In my landscape its centralhub deployment...so how will the cached url will be useful.

      for me the error is:

      smrt.JPG

      Regards,

      Vishnu

      Author's profile photo Vishnu Pankajakshan Panicker
      Vishnu Pankajakshan Panicker

      I internally changed the hostname of GW with ERP.Now its working...

      Regards,

      Vishnu

      Author's profile photo Chandrashekhar Mahajan
      Chandrashekhar Mahajan
      Blog Post Author

      yes you figured out the issue correctly as the temp. url is getting generated in the backend system and hence that will not work on GW server. other solution could be to export xstring from the backend from your FM for e.g. FM z_test_pdf_display

      and then use the logic of generating the temp. url on GW hub system so that it will use hostname of Hub system.

      But instead of doing all this, please use the improved approach of using media stream (get_stream) method to display PDF.

      Regards,

      Chandra

      Author's profile photo Former Member
      Former Member

      Hi Chandrashekhar Mahajan,

      Thanks for your blog. It worked pretty well. I'm able to view the PDF in iOS but not in android.

      and the url

      http:<server>:<domain>/sap/....../Employee('xyz')/$value 

      is downloading the pdf automatically but not embedding inside the iframe as you suggested.

      Kindly help

      Karthik J

      Author's profile photo Deepak Sharma
      Deepak Sharma

      Hi Karhik,

      Have you found solution for embedding instead of downloading pdf. In our case pdf was opening as embedded till we upgraded UI5 & Gateway. After upgrade to Gateway SP 11 and UI5 1.28, pdf is downloading instead of opening as embedded.

      Thanks

      Deepak

      Author's profile photo Former Member
      Former Member

      I had that issue after upgrading GW as well, and the solution for this is that GW is setting the HTTP header differently, so if you don't set it yourself it will download instead of display it.


      In your GET_STREAM method:


      DATA: ls_header TYPE ihttpnvp.

      ls_header-name = 'Content-Disposition'.

      ls_header-value = |inline; filename="{ lv_filename }"|.

      set_header( is_header = ls_header ).


      Hope it helps 🙂

      Author's profile photo Deepak Sharma
      Deepak Sharma

      Thanks Jeferson.

      Solved by setting header as suggested.

      Author's profile photo Dhananjay Choubey
      Dhananjay Choubey

      Can you please tell me what is lv_filename here from the code? Thanks. 🙂

      Author's profile photo billy shen
      billy shen

      Hi Chandrashekhar,

         i followed the Improved and Recommended approach to display paf,it ok with Firefox and gateway client, but not ok on Chrome, Safiri. on Chrome the pdf didn't generate in the Z_TEST_PDF_DISPLAY, when it  CALL FUNCTION lv_fm_name ,the ls_output_data is empty. On Firefox it has data. not sure why. do you have any ideas?  what should do with http header or cache response, i tried lots of codes, still not working.

      thanks,

      Billy

      8E4632D6-216F-456E-BE27-C6862EC59666.pngA410868E-2886-4B01-8B3B-D22DB999C1A7.png

      Author's profile photo Former Member
      Former Member

      Hi Chandrasekhar,

                       Excellent blog and well documented.  We followed same approach to display the PDF in UI5 using Smart Form.  We are getting 404 error when we use your first approach (generating URL in ECC) instead Stream one.  First time it is 404 error and if I refresh after 8 seconds later we are able to see the pdf.   If I refresh it is not diplaying and getting 404.  The issue is intermittent.  Could you please help to resolve the issue.  Thank you.404.JPG

      With Regards

      Sudheer.

      Author's profile photo Michael Appleby
      Michael Appleby

      Please create a new Discussion marked as a Question.  Comments to a blog is not the right vehicle for asking questions as the results are not easily searchable.  Once your issue is solved, a Discussion with make the results much more visible to others experiencing a similar problem.

      Thanks, Mike (Moderator)

      SAP Technology RIG

      Author's profile photo CHIRAG MAKWANA
      CHIRAG MAKWANA

      Great Tutorial ,Chandrashekhar Mahajan sir

      completely follow your blog and, achieved success over that. 

      I have issue on naming convention while downloading PDF through browser ,

      Every time I click the link in browser, its generate 'Entity.pdf',

      how can i specify unique name to that generating PDF with using OData Services.


      >> /sap/opu/odata/sap/ZTESTPDF_SRV/pdfset('501')/$value

      working well, but

      this link show me entity.pdf as a downloaded file.

      how can we convert name entity.pdf to 501.pdf with OData services

      please help me on this..



      Thanks, Chirag Makwana.

      Author's profile photo Chandrashekhar Mahajan
      Chandrashekhar Mahajan
      Blog Post Author

      Hi Satyaki,

      Please create a new Discussion marked as a Question.  Comments to a blog is not the right vehicle for asking questions as the results are not easily searchable.  Once your issue is solved, a Discussion with make the results much more visible to others experiencing a similar problem.

      Thanks,

      Chandra(Moderator)

      Author's profile photo Deepak Sharma
      Deepak Sharma

      Using iFrame src="pdf location" is actually making multiple calls to SAP gateway. I guess one call is by model.read and another if by iFrame to actually display the pdf content.

      You can place session debugger in DPC class & you can see that it is hit 2 times.Capture.JPG

      Is there a way to avoid making 2 calls ?

      Screenshot from browser making multiple calls is attached.

      Thanks

      Deepak

      Author's profile photo Former Member
      Former Member

      Hi Chandrasekhar,

      I am trying to use the smartform in my app and i am using the read function just like you suggested, but for some reason i get a "No Handler for data" error. I also made a post where you can see all the things that I tried. The specified item was not found.

      Maybe you can tell me what I did wrong. I hope you can help me,

      Kind regards,

      Arun

      Author's profile photo Elie H
      Elie H

      Hi Arum,

      I face on the same issue.

      Did you solve it?

      Best regards,

      Elie

      Author's profile photo Juan Camilo Jaramillo
      Juan Camilo Jaramillo

       

      hi Elie H,

      I get the same error. could you solved it?

       

      Thanks,

      Author's profile photo Amber .
      Amber .

      Hi Chandreshekar,

      Very Informative blog but I have a small question. Even if we don't declare MIME type as property in the entity "pdf" and accordingly comment the line for setting it as content type , it will still work , right?? Since the get_stream method will return MIME object , what is the need to have mime property defined in our entity PDF as we are not using pdf_get_entity at all 🙂

      Just had the above question in mind since I tried removing the above part as I mentioned and it still works (also same thing with MIME images)

      Author's profile photo Former Member
      Former Member

      Hi Chandrashekhar,

      Thanks for explaining and sharing code to display smartform using OData and consume it in SAPUI5. I am able to consume smartform in my SAPUI5 application.

      When I executed the SAPUI5 application, I observed that OData service is being called three times. I guess one when "oModel.Read" is being called, second when iframe is being loaded and third time may be when content is being loaded.

      Is there any way to restrict the service call to single call?

      Once again, thank you so much for this blog

      Regards,

      Sid

      Author's profile photo Vijay Sah
      Vijay Sah

      Hi Chandrasekhar,

      This post really helped me.

      Thanks a lot.

      Regards,

      Vijay

      Author's profile photo Former Member
      Former Member

      Hi Chandrasekhar,

      This blog really helped me, thank you. I have a question though,

      Is there any way to change the window title on which pdf is displayed?

      I used the new methodology to display PDF in a new pop-up window, but the window title/name shows "$value". Even if I display it within the application view, it always says "$value.

      in my controller I have

      parent.window.open(pdfURL, "PDF"), but the "PDF" string doesn't seem to have any effect.

      Regards,
      Gonçalo

      Author's profile photo Former Member
      Former Member

      Hi Gonçalo! How are you?
      About how to change or remove the window title or name ($value) when a pdf is displayed at a UI5 application. Did you can? If yes can you show how did you fix it? Thank you!

      Author's profile photo Former Member
      Former Member

      Hi Chandreshekar,

      this is a very good blog but i have one question, because I´m running in an error.

      I have implemented the backend just as you described and i can see in the debugger that the method GET_STREAM is called and returns a stream.

      But in the frontend i get this error:

      sap-ui-core.js:802017-01-27 09:31:36 The following problem occurred: no handler for data –

      Any idea how to solve this problem?

       

      Thanks,

       

      Mathias

       

      Found solution on my own.

      Author's profile photo Lakshmi Channakeshavaiah Jaya
      Lakshmi Channakeshavaiah Jaya

      hi Mathias,

      I get the same error. Could you please tell me how you solved it?

       

      Thanks,

      Lakshmi

      Author's profile photo Juan Camilo Jaramillo
      Juan Camilo Jaramillo

       

      hi Lakshmi,

      I get the same error. could you solved it?

       

      Thanks,

      Author's profile photo Ketan Badnore
      Ketan Badnore

      Hi Chandrashekhar,

      Thanks for this informative blog on media streaming.

      I have a doubt here. What is the need to redefine method - 'Define' from MPC_EXT class. I tried this approach without redefining the 'Define' method and is working fine.

      Could you please let me know its significance.

       

      -Thanks.

      Author's profile photo Former Member
      Former Member

      Hi Chandra,

       

      Any idea on how to implement the same with UI5 Application deployed on JAVA AS and without NW Gateways services.

      Author's profile photo Jalpa Desai
      Jalpa Desai

      Hi,

       

      I have followed exactly the same steps as above. The original steps, not the improved steps. I am executing the application from my fiori launchpad tile, but i am unable to view the pdf.

      What could I have missed out? Kindly suggest.

       

      Thanks & Regards,

      Jalpa Desai

       

      Author's profile photo Former Member
      Former Member

      Hi Chandra,

      I have used the 2nd approach given in the blog and try to view the pdf in the Fiori application, but i am getting error “invalid MIME part type “. Can you please help?

      Apart for this in the entity, the Mimetype is defined as XSTRINGVAL. Is this correct?

       

      Thanks and regards,

      Pankaj

      Author's profile photo sabari vignesh giri raj kumar
      sabari vignesh giri raj kumar

      Hi Chandrashekhar,

      I have been working on the SAPUI5 and I try to get a PDF File from backend when we test it in the SAP Gateway Client “EntitySet()/$value” it’s working

      ~status_code 200

      ~status_reason OK

      but in button press function the oModel.read code gives an error.

      Cannot read property ‘body’ of undefined

      My button code :

      function() {

      var value = this.getView().byId(“inputValue”).getValue();

      var sRead = “/PrintProcessSet(Tknum='” + value + “‘)/$value”;

      var html = new sap.ui.core.HTML();

      var oModel = this.getView().getModel();

      oModel.read(sRead, {

      success: function(oData, oResponse) {

      var response = oResponse.requestUri;

      html.setContent(“<iframe src=” + response + ” width=’700′ height=’700′></iframe>”);

      var windows = window.open(“”, “My PDF”, ” width=’700’ height=’700′”);

      windows.document.write(html);

      windows.print();

      windows.close();

      },

      error: function() {

      alert(“Read failed”);

      }

      });

      },

      We get the Following error

      Thanks and regards,

      Sabari.

      Author's profile photo Yen Shen Lim
      Yen Shen Lim

      Hi all,

      I'm having the same issue, any inputs for this error?

      Thanks.

      Regards,

      Ben

      Author's profile photo David Raven
      David Raven

      Hi,

       

      Is it possible to use your tutorial to download not only PDF but any file we want?

      I think the answer is yes as long that we return xstring.

      Can you confirm?

       

      Thank you.

       

      Regards.

      Author's profile photo Gurrala Venkata Rahul Reddy
      Gurrala Venkata Rahul Reddy

       

      Hi ChandraSekhar,

      where can i find the link for Improved And Recommended approach to display pdf

       

      Thanks In advance

      Author's profile photo Mahesh kumar
      Mahesh kumar

      Hi ChandraSekhar,

       

      Nice Blog, But we are implemented same but its not working, we are facing error like PDFSET_entityset is not implemented, So please help me.

       

      Thanks,

      Mahesh