Skip to Content

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

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 ).

Solution in summary,

  • PDF metadata is used to set 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.
To report this post you need to login first.

4 Comments

You must be Logged on to comment or reply to a post.

  1. 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

    (0) 
  2. 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

    (0) 

Leave a Reply