Using a Web Service to get PDF content from SAP and display on a Web Page
Have you ever wanted to get content from SAP and display it as a PDF on a web page? Let’s say you want to develop a web site using .Net technologies and communicate with SAP. One of your requirements might be to send content to the web (from SAP) for display as a PDF document. A good example could be displaying a benefits statement or a pay statement of some kind. I’ll discuss here an example of taking content from the SAP Spool and generating PDF content for display on the web. This could easily be changed to generate PDF content from just about anything stored in SAP (SAPSCRIPT, SMARTFORMS, ATTACHMENTS, etc..). The web site will communicate with SAP via web services.
Step 1: Create an RFC Enabled Function Module with the following code:
function z_spool_to_pdf.
*”———————————————————————-
*”*”Local Interface:
*” IMPORTING
*” VALUE(I_SPOOL) TYPE RQIDENT
*” EXPORTING
*” VALUE(O_XSTRING) TYPE XSTRING
*” VALUE(O_SUCCESS_IND) TYPE CHAR1
*” VALUE(RS_RETURN) TYPE ZCSS_ST_RETURN (not really needed)
*”———————————————————————-
data: err type string.
data: g_client type rstsclient,
g_name type rstsoname,
g_objtype type rststyname,
g_type type rststyname,
g_is_otf type c,
pdf type standard table of tline.
data: g_numbytes type i,
g_arc_idx type toa_dara,
g_pdfspoolid type rspoid,
g_jobname type btcjob,
g_jobcount type btcjobcnt.
data: begin of t_text occurs 0,
line(134) type c,
end of t_text.
data: l_tsp01 type tsp01.
data: oref type ref to cx_root.
clear o_success_ind.
try.
if not ( i_spool is initial ).
select single * from tsp01 into l_tsp01 where rqident = i_spool.
if sy-subrc <> 0.
o_success_ind = ‘N’.
endif.
g_client = l_tsp01-rqclient.
g_name = l_tsp01-rqo1name.
call function ‘RSTS_GET_ATTRIBUTES’
exporting
authority = ‘SP01’
client = g_client
name = g_name
part = 1
importing
type = g_type
objtype = g_objtype
exceptions
fb_error = 1
fb_rsts_other = 2
no_object = 3
no_permission = 4.
if g_objtype(3) = ‘OTF’.
g_is_otf = ‘X’.
else.
g_is_otf = space.
endif.
clear: pdf. refresh: pdf.
if g_is_otf = ‘X’.
call function ‘CONVERT_OTFSPOOLJOB_2_PDF’
exporting
src_spoolid = l_tsp01-rqident
no_dialog = ‘ ‘
importing
pdf_bytecount = g_numbytes
pdf_spoolid = g_pdfspoolid
btc_jobname = g_jobname
btc_jobcount = g_jobcount
tables
pdf = pdf
exceptions
err_no_otf_spooljob = 1
err_no_spooljob = 2
err_no_permission = 3
err_conv_not_possible = 4
err_bad_dstdevice = 5
user_cancelled = 6
err_spoolerror = 7
err_temseerror = 8
err_btcjob_open_failed = 9
err_btcjob_submit_failed = 10
err_btcjob_close_failed = 11.
if sy-subrc ne 0.
o_success_ind = ‘N’.
endif.
else.
call function ‘CONVERT_ABAPSPOOLJOB_2_PDF’
exporting
src_spoolid = l_tsp01-rqident
no_dialog = ‘ ‘
importing
pdf_bytecount = g_numbytes
pdf_spoolid = g_pdfspoolid
btc_jobname = g_jobname
btc_jobcount = g_jobcount
tables
pdf = pdf
exceptions
err_no_abap_spooljob = 1
err_no_spooljob = 2
err_no_permission = 3
err_conv_not_possible = 4
err_bad_destdevice = 5
user_cancelled = 6
err_spoolerror = 7
err_temseerror = 8
err_btcjob_open_failed = 9
err_btcjob_submit_failed = 10
err_btcjob_close_failed = 11.
if sy-subrc ne 0.
o_success_ind = ‘N’.
endif.
endif.
t_text[] = pdf[].
call function ‘SCMS_BINARY_TO_XSTRING’
exporting
input_length = g_numbytes
importing
buffer = o_xstring
tables
binary_tab = t_text
exceptions
failed = 1
others = 2.
if sy-subrc <> 0.
o_success_ind = ‘N’.
endif.
else.
o_success_ind = ‘N’.
endif.
catch cx_root into oref.
err = oref->get_text( ).
o_success_ind = ‘N’.
endtry.
if o_success_ind is initial.
o_success_ind = ‘Y’.
endif.
endfunction.
Step 2: Create a web service of your function module:
There are just so many options for generating a web service from you RFC module that I will leave that up to you. You could use the built-in SAP web service functionality or you could use something else. I’m using the .Net connector for this example. Once you have built your web service for this function module, proceed to Step 3.
Step 3: The Web Code:
Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO
Public Class Z_SPOOL_TO_PDF1
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim conStr As String = “ASHOST=<your host> SYSNR=?? CLIENT=??? USER=???????? PASSWD=????????”
Dim obj_pdf As Object
Dim O_SUCCESS_IND As String
Dim O_RETURN_STRUC As ZCSS_ST_RETURN
Dim RFC_Z_SPOOL_TO_PDF As New Z_SPOOL_TO_PDF(conStr)
‘hard code the spool number (30283) for this example
RFC_Z_SPOOL_TO_PDF.Z_Spool_To_Pdf1(“30283”, O_SUCCESS_IND, obj_pdf, O_RETURN_STRUC)
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.AppendHeader(“Content-Type”, “application/pdf”)
HttpContext.Current.Response.AppendHeader(“Content-ID”, “filename.pdf”)
HttpContext.Current.Response.AppendHeader(“Content-Transfer-Encoding”, “Binary”)
HttpContext.Current.Response.AppendHeader(“Content-Disposition”, “attachment; filename=””” & “filename.pdf” & “”””)
HttpContext.Current.Response.BinaryWrite(obj_pdf)
HttpContext.Current.Response.End()
End Sub
End Class
Testing this, results in:
VERY cool!
Hi,
Thank you very much this is very useful.
Is there also a way that I can post a pfd to the web-service.
What I mean is the other way around.
Greets Julian
Useful Information.
Regards,
Giir
Nice...
Very helpful, thanks.
Hi Philip,
We have a similar requirement. Can you please let me know where the web code given the your exp will be written.
Regards,
Priyaranjan
Hello Priyaranjan, do you discovery where the web code should be writen?