cancel
Showing results for 
Search instead for 
Did you mean: 

Convert BSP output to PDF

Former Member
0 Kudos

Hi All,

I have a BSP application wherein a Dashboard report is presented. basically it has a selection screen and a tableview which is displayed. There are two buttons to decide whether its a management report or a regular report. Depending upon the buttons the output inthe table changes. This part is working fine. In case of management report i have to color some cells based on a particular condition. There is another button for ' Export to PDF'. I need to convert the output of the tableview to PDF. I could not find a direct way to do , so I created an ABAP report of similair output called Z01_BSPPRDDASH_SPOOL . when user presses the button, the report output is genereated and sent to spool. The spool is converrted to PDF using the std FM 'CONVERT_ABAPSPOOLJOB_2_PDF'. After this i need to display the pdf. but the code given below is not doing that. I am unable to see any output. Can you pls telle me what is going wrong . Also let me know if my approach is correct. Can there be any other way of converting a tableview to pdf. I need to solve this urgently as my delivery date is nearing for this object.

Code :

DATA: ITAB TYPE TLINE,

WITAB TYPE TABLE OF TLINE,

OUTPUT TYPE STRING ,

  • CONTENT TYPE XSTRING,

WF_SPONO TYPE TSP01-RQIDENT ,

WF_PARAMS TYPE PRI_PARAMS,

VALID_FLAG(1) TYPE C ,

LC_RQ2NAME TYPE TSP01-RQ2NAME ,

TSP01_WA TYPE TSP01 .

DATA: CACHED_RESPONSE TYPE REF TO IF_HTTP_RESPONSE.

DATA: RESPONSE TYPE REF TO IF_HTTP_RESPONSE.

DATA: GUID TYPE GUID_32.

DATA: L_PDF_XSTRING TYPE XSTRING,

  • lt_lines type table of tline,

LS_LINE TYPE TLINE,

DISPLAY_URL TYPE STRING,

L_PDF_LEN TYPE I.

DATA: OUTPUT_OPTIONS TYPE SSFCOMPOP,

CONTROL_PARAMETERS TYPE SSFCTRLOP,

OUTPUT_DATA TYPE SSFCRESCL,

DEVTYPE TYPE RSPOPTYPE.

*

CALL FUNCTION 'GET_PRINT_PARAMETERS'

EXPORTING

DESTINATION = 'LOCL'

IMMEDIATELY = ' '

RELEASE = ' '

NEW_LIST_ID = 'X'

LINE_SIZE = 255

LINE_COUNT = 65

LAYOUT = 'X_65_255'

SAP_COVER_PAGE = 'X'

RECEIVER = 'SAP*'

DEPARTMENT = ''

NO_DIALOG = 'X'

IMPORTING

OUT_PARAMETERS = WF_PARAMS

VALID = VALID_FLAG.

IF VALID_FLAG = 'X' AND SY-SUBRC = 0.

SUBMIT Z01_BSPPRDDASH_SPOOL TO SAP-SPOOL WITHOUT SPOOL DYNPRO

SPOOL PARAMETERS WF_PARAMS

AND RETURN .

ENDIF.

  • this is to get the spool no. created. Not a real nice

  • way though. if someone knows a better method please do

  • share

CONCATENATE 'SAPMHTTP'

SY-UNAME+0(3)

INTO LC_RQ2NAME SEPARATED BY '_'.

SELECT * FROM TSP01 INTO TSP01_WA WHERE RQ2NAME = LC_RQ2NAME AND

RQOWNER = SY-UNAME

ORDER BY RQCRETIME DESCENDING.

WF_SPONO = TSP01_WA-RQIDENT.

EXIT.

ENDSELECT.

IF SY-SUBRC NE 0.

CLEAR WF_SPONO.

ENDIF.

IF NOT WF_SPONO IS INITIAL.

CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'

EXPORTING

SRC_SPOOLID = WF_SPONO

NO_DIALOG = 'X'

TABLES

PDF = WITAB

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 = 8

ERR_BTCJOB_SUBMIT_FAILED = 9

ERR_BTCJOB_CLOSE_FAILED = 10

OTHERS = 11.

ENDIF.

IF SY-SUBRC EQ 0.

LOOP AT WITAB INTO ITAB .

TRANSLATE ITAB USING ' ~'.

CONCATENATE OUTPUT ITAB INTO OUTPUT IN CHARACTER MODE.

ENDLOOP.

TRANSLATE OUTPUT USING '~ ' .

CREATE OBJECT CACHED_RESPONSE TYPE CL_HTTP_RESPONSE EXPORTING

ADD_C_MSG = 1.

L_PDF_LEN = STRLEN( OUTPUT ).

CACHED_RESPONSE->SET_CDATA( DATA = OUTPUT

LENGTH = L_PDF_LEN ).

CACHED_RESPONSE->SET_HEADER_FIELD( name =

if_http_header_fields=>content_type

VALUE = 'application/pdf' ).

CACHED_RESPONSE->SET_STATUS( CODE = 200 REASON = 'OK' ).

CACHED_RESPONSE->SERVER_CACHE_EXPIRE_REL( EXPIRES_REL = 180 ).

CALL FUNCTION 'GUID_CREATE'

IMPORTING

EV_GUID_32 = GUID.

CONCATENATE RUNTIME->APPLICATION_URL '/' GUID '.pdf'

INTO DISPLAY_URL.

CL_HTTP_SERVER=>SERVER_CACHE_UPLOAD( URL = DISPLAY_URL

RESPONSE = CACHED_RESPONSE ).

navigation->goto_page( display_url ).

.

RETURN .

Regards,

Sriprabha

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Graham,

Thanks a ton for the information. Now the code seems to work a bit. Still an issue, if this is solved then my problem would be solved. As mentioned by you, I am not using cached_response but mresponse an object which is already available at runtime. When the button is pressed for Export to PDF , I get a popup to save a test.pdf, but when i open the file i get an error saying that " Acrobat could not open the pdf file because it is either not supported type or the file is damaged". I feel that error may be because I am not converting the PDF correctly. I am attaching my code for the part of conversion. Please check and let me know if I am wrong some place. Please check if the pdf data is getting converted correctly.

CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'

EXPORTING

SRC_SPOOLID = WF_SPONO

NO_DIALOG = 'X'

TABLES

PDF = WITAB

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 = 8

ERR_BTCJOB_SUBMIT_FAILED = 9

ERR_BTCJOB_CLOSE_FAILED = 10

OTHERS = 11.

ENDIF.

IF SY-SUBRC EQ 0.

LOOP AT WITAB INTO ITAB .

  • TRANSLATE ITAB USING ' '.

CONCATENATE OUTPUT ITAB-tdline INTO OUTPUT.

  • IN CHARACTER MODE.

ENDLOOP.

  • TRANSLATE OUTPUT USING '~ ' .

call function 'SCMS_STRING_TO_XSTRING'

EXPORTING

text = output

IMPORTING

buffer = outputx.

mresponse->set_data( data = outputx ).

mresponse->set_header_field( name = 'content-type'

value = 'application/pdf' ).

  • some Browsers have caching problems when loading PDF format

mresponse->set_header_field(

name = 'cache-control'

value = 'max-age=0' ).

CONCATENATE 'attachment; filename=' 'test.pdf'

INTO display_url.

mresponse->set_header_field(

name = 'content-disposition'

value = display_url ).

navigation->response_complete( ).

Regards,

Sriprabha

GrahamRobbo
Active Contributor
0 Kudos

Hi Srippy,

what an interesting problem. I haven't been able to solve it but I can tell you what the problem is.

Firstly take a look at any Adobe Acrobat file. If you open it with a text editor you will see that the very beginning of it looks like this...

%PDF-1.3

This is essentially an identifier that says I am a document of type PDF and version 1.3. Also we need to understand that a PDF file contains both textual data and raw binary data.

Now if you use the debugger to look at the first line in the internal table data returned from the call to CONVERT_ABAPSPOOLJOB_2_PDF you will see something like...

TDFORMAT ={}
TDLINE   {}{_*(^ (JUNK),ndn,sda...

Of more interest is the hexadecimal representation of the data.

TDFORMAT 0x25504446
TDLINE   0x2D312E330D0A25E2E3CFD30D0A3220...

which, if I convert each 8 bits to ASCII gives me

TDFORMAT %PDF
TDLINE   -1.3 cr lf ...

So firstly, the PDF data is stored in BOTH the TDFORMAT and TDLINE fields of the itab. Therefore your concatenate statement should be more like this.

CONCATENATE output itab-tdformat itab-tdline INTO output.

But we have another problem. String data is stored with one character per byte. On my system this is 16 bits. So this means, for example, that %PDF should be stored as 0x0025005000440046 and not as 0x25504466. Binary data, on the other hand is stored "packed" so it would be 0x25504466. You can see what I mean if you use the debugger to step through this code fragment and look at the hex values of the two variables.

DATA: wa_string  TYPE string VALUE '%PDF-1.3',
      wa_stringx TYPE xstring.
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
  EXPORTING
    text   = wa_string
  IMPORTING
    buffer = wa_stringx.

So, my thought is that the function module CONVERT_ABAPSPOOLJOB_2_PDF should really be returning the result in a byte string data structure.

Anyway, the problem now is how to grab each 8-bits from the character string and plonk it into a binary string. I haven't yet figured out how to do this. I am asking a few other for ideas but it might be worth starting another tthread in the ABAP Objects forum.

Cheers

Graham Robbo

GrahamRobbo
Active Contributor
0 Kudos

Hi Srippy,

try this.

DATA: witab       TYPE TABLE OF tline,
      itab        TYPE tline,
      outputx     TYPE xstring,
      display_url TYPE string.

FIELD-SYMBOLS: <p> TYPE x.

CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
  EXPORTING
    src_spoolid              = 3831
    no_dialog                = 'X'
  TABLES
    pdf                      = witab
  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
    OTHERS                   = 12.

IF sy-subrc = 0.

  LOOP AT witab INTO itab .
    ASSIGN itab TO <p> CASTING TYPE x.
    CONCATENATE outputx <p> INTO outputx IN BYTE MODE.
  ENDLOOP.

  response->set_data( data = outputx ).

  response->set_header_field( name = 'content-type'
                              value = 'application/pdf' ).

  response->set_header_field( name = 'cache-control'
                              value = 'max-age=0' ).

  CONCATENATE 'attachment; filename=' 'test.pdf'
    INTO display_url.

  response->set_header_field( name = 'content-disposition'
                              value = display_url ).

  navigation->response_complete( ).

ENDIF.

Cheers

Graham Robbo

Answers (5)

Answers (5)

Former Member
0 Kudos

Hi Graham,

Thankx a million for the answer. Its now working fine and properly. Your code and the explaination really helped.

Regards,

Sriprabha

reza_omranzade
Explorer
0 Kudos

hi expert ,
i use this code and it work great , but i have one problem when i export bsp page to pdf some arabic word did not show well and they are discorrect and wrong format .

is there any way that i can export arabic word to pdf ?

thanks alot .

best regards ,

reza .

Former Member
0 Kudos

Hi Graham ,

Thanks for the code. But right now I am getting a Blank output. Could you tell me what could be wrong.

Following is my code :

DATA: CACHED_RESPONSE TYPE REF TO IF_HTTP_RESPONSE.

IF NOT WF_SPONO IS INITIAL.

CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'

EXPORTING

SRC_SPOOLID = WF_SPONO

NO_DIALOG = ' '

TABLES

PDF = WITAB

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 = 8

ERR_BTCJOB_SUBMIT_FAILED = 9

ERR_BTCJOB_CLOSE_FAILED = 10

OTHERS = 11.

ENDIF.

IF SY-SUBRC EQ 0.

LOOP AT WITAB INTO ITAB .

TRANSLATE ITAB USING ' ~'.

CONCATENATE OUTPUT ITAB INTO OUTPUT IN CHARACTER MODE.

ENDLOOP.

TRANSLATE OUTPUT USING '~ ' .

CREATE OBJECT CACHED_RESPONSE TYPE CL_HTTP_RESPONSE EXPORTING

ADD_C_MSG = 1.

L_PDF_LEN = STRLEN( OUTPUT ).

CACHED_RESPONSE->SET_CDATA( DATA = OUTPUT

LENGTH = L_PDF_LEN ).

CACHED_RESPONSE->SET_HEADER_FIELD( name = 'content-type'

VALUE = 'application/pdf' ).

  • some Browsers have caching problems when loading PDF format

CACHED_RESPONSE->set_header_field(

name = 'cache-control'

value = 'max-age=0' ).

CONCATENATE RUNTIME->APPLICATION_URL '/' 'test.pdf'

INTO DISPLAY_URL.

CACHED_RESPONSE->set_header_field(

name = 'content-disposition'

value = DISPLAY_URL ).

navigation->response_complete( ).

Regards,

Sriprabha

GrahamRobbo
Active Contributor
0 Kudos

Hi Srippy,

dump all your references to CACHED_RESPONSE. You don't need any of this.

The <i>response</i> object is created by the HTTP runtime and is already available to you.

Just use my code unaltered and plonk in the string variable that contains the .PDF data.

Cheers

Graham Robbo

GrahamRobbo
Active Contributor
0 Kudos

Hi Srippy,

rather than dump the PDF file into the cache and redirect to it you can just return it directly to the browser like this.

        response->set_cdata( data = pdf_data ).

        response->set_header_field( name  = 'content-type'
                                    value = 'application/pdf' ).

* some Browsers have caching problems when loading PDF format
        response->set_header_field(
                           name  = 'cache-control'
                           value = 'max-age=0' ).

        CONCATENATE 'attachment; filename=' file_name '.pdf'
        INTO contdisp.

        response->set_header_field(
                           name  = 'content-disposition'
                           value = contdisp ).

        navigation->response_complete( ).

Cheers

Graham Robbo

Former Member
0 Kudos

I have checked that weblog, but i couldnt find the solution. I guess something is missing in the code, but i am not able to find what is the problem.

Former Member
0 Kudos

Hi,

Did you check /people/sap.user72/blog/2004/11/10/bsphowto-generate-pdf-output-from-a-bsp

Amandeep