This blog discusses the tricks to avoid the frequent issues with PDF files in Fiori applications.
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.
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.