Skip to Content
Technical Articles
Author's profile photo Krishna Kishor Kammaje

SAP Fiori tricks: Get rid of $value in PDF display/downloads

This blog discusses the tricks to avoid the frequent issues with PDF files in Fiori applications.

Problems

Whenever there is a PDF download or display involved in Fiori applications, we have always encountered the annoying ‘$value’ as PDF’s title or suggested file name while saving the PDF. See the below screenshots showing the issue.

Below screenshot from the Fiori demo site (Paystub app) shows the PDF title appearing as $value in Chrome browser.

 

Below screenshot shows the proposed filename $value, while saving the PDF that was displayed on the browser from the Gateway server.

 

Solution

Solution Summary:

  • PDF metadata is updated with the right ‘Title’. This will get rid of $value displayed as PDF’s title.
  • ‘Content-Disposition’ HTTP header is used to influence the proposed filename while downloading.

Prerequisites:

Adobe Document Server (ADS) should be configured. This is a JAVA Add-In to your ABAP server. This comes as part of your NW licencing as long as you do not provide the ability to edit (interact) the PDF file. You can use SAP’s test program ‘FP_PDF_TEST_23’ for testing this configuration.

Code:

Below code in method /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_STREAM of DPC_EXT class resolves these issues. Inline comments in the code are self-explanatory.

* lv_pdf_data contains the binary PDF data. (XSTRING)

    DATA: ls_meta   TYPE sfpmetadata,
          lx_fpex   TYPE REF TO cx_fp_runtime,
          lo_fp     TYPE REF TO if_fp,
          lo_pdfobj TYPE REF TO if_fp_pdf_object.

* add metadata to PDF document
    TRY.
        "Create PDF Object.
        lo_fp = cl_fp=>get_reference( ).
        lo_pdfobj = lo_fp->create_pdf_object( connection = 'ADS' ).
        lo_pdfobj->set_document( pdfdata = lv_pdf_data ).
        "Set title.
        ls_meta-title = 'My PDF File Title'.
        lo_pdfobj->set_metadata( metadata = ls_meta ).
        lo_pdfobj->execute( ).
        "Get the PDF content back with title
        lo_pdfobj->get_document( IMPORTING pdfdata = ls_stream-value ).

      CATCH cx_fp_runtime_internal
            cx_fp_runtime_system
            cx_fp_runtime_usage INTO lx_fpex.
    ENDTRY.

    "'application/pdf' is required to inform the browsers that 
    "this is a PDF file so that it can be opened in the default 
    "PDF viewer of the browser.
    ls_stream-mime_type = 'application/pdf'.

    copy_data_to_ref( EXPORTING is_data = ls_stream
                      CHANGING  cr_data = er_stream ).

    "'Content-Disposition: inline' directs the browsers to preview 
    "the PDF instead of downloading the file.
    "'filename' is required by Chrome to suggets the file name while downloading.
    "IE does not respect the 'filename' with 'inline', but respects 
    "when Content-Disposition is 'attachemnt'
    ls_lheader-name = 'Content-Disposition'.
    ls_lheader-value = |inline; filename="MyFileName.pdf"|.
    set_header( is_header = ls_lheader ).

Assigned Tags

      6 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Raju C D
      Raju C D

       

      Hi Krishna,

       

      Nice one.. Thanks for Sharing ..

      Regards,

      Raju C D

       

      Author's profile photo Lakshmaiah Ravilla
      Lakshmaiah Ravilla

       

      Hi Krishna,

      I did as per your shared document still i face same issue of $ value in PDF viewer and after opening pdf i try to download from download button. its shows error file format not support or file encrypted.

      Note: I passed smartform xsting to lv_pdf_data.

      Could kindly help me out to resolve on that.

      Thanks

      Ravilla

      Author's profile photo Saket Amraotkar
      Saket Amraotkar

      Krishna Kishor Kammaje,

       

      Thanks for nice blog.

      It worked on all version below NW 7.51.

      Earlier i implemented this code on NW 7.4 it worked fine.

      But same code when i tried in NW 7.51, its only resolved saving filename issue (Download), but still shows $value on title of pdf (Display).

      Any tip you would like to give here?

       

      Regards

      Saket

      Author's profile photo M. Happé
      M. Happé

      Thnx very usefull blog, to set the title of the PDF

      Author's profile photo LAXMAN GOLLA
      LAXMAN GOLLA

      Hi,

      Thank you it is very usefull blog.

      I have one query here we can change $value to PDF title name. it is good but my present requirement is user don't want see PDF File default buttons like Print button and Download buttons. Can you please let me know how to hide that thous buttons.

      Thanks advance.

      Author's profile photo Rahul MAHAJAN
      Rahul MAHAJAN

      Hi Laxman,

       

      Did you found any solution this ?

       

      Thanks,

      Rahul