cancel
Showing results forΒ 
Search instead forΒ 
Did you mean:Β 

PDF file gets corrupted when generated from BSP

Former Member
0 Kudos

Hi,

I am generating a PDF file from a BSP page ( see the code below). The problem is that PDF file gets corrupted and I am unable to open it. I also tried to use other functions like GUI_Download and DOwnload as suggested by many and they also do not work either. Has any one generated a PDF from BSP and can some one help?

Data: GG type String.

  • event handler for data retrieval

data : event type ref to cl_htmlb_event.

class cl_htmlb_manager definition load.

class CL_HTTP_RESPONSE DEFINITION LOAD.

DATA : PARAMS TYPE PRI_PARAMS,

VALID TYPE C,

GV_SUCCESS_SPOOL_NO LIKE SY-SPONO.

CALL FUNCTION 'GET_PRINT_PARAMETERS'

EXPORTING DESTINATION = 'LOCL'

  • COPIES = COUNT

  • LIST_NAME = 'TEST'

  • LIST_TEXT = 'Test NEW-PAGE PRINT ON'

IMMEDIATELY = ''

RELEASE = 'X'

NEW_LIST_ID = 'X'

  • EXPIRATION = DAYS

LINE_SIZE = 140

  • LINE_COUNT = 23

  • LAYOUT = 'X_PAPER'

  • SAP_COVER_PAGE = 'X'

  • RECEIVER = 'SAP*'

  • DEPARTMENT = 'System'

NO_DIALOG = 'X'

IMPORTING OUT_PARAMETERS = PARAMS

VALID = VALID.

IF VALID <> SPACE.

NEW-PAGE PRINT ON PARAMETERS PARAMS NO DIALOG.

write πŸ˜• 'Hello Message1'.

write πŸ˜• 'Hello Message2 '.

NEW-PAGE PRINT OFF .

ENDIF.

data : spool_no TYPE TSP01-RQIDENT .

spool_no = sy-spono.

DATA: itab TYPE TLINE,

witab TYPE TABLE OF TLINE,

output TYPE STRING,

outputx TYPE XSTRING.

data : l_pdf_len type i.

data: mi_bytecount type i.

DATA : APP_TYPE TYPE STRING VALUE 'APPLICATION/PDF' .

DATA: cached_response TYPE ref to if_HTTP_RESPONSE.

data : l_display_url type string .

data : guid type GUID_32 .

call function 'CONVERT_ABAPSPOOLJOB_2_PDF'

EXPORTING

SRC_SPOOLID = spool_no

NO_DIALOG = 'X'

IMPORTING

PDF_BYTECOUNT = mi_bytecount

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.

if sy-subrc EQ 0.

LOOP AT witab INTO itab.

CONCATENATE

output

itab-tdline

INTO output in character mode.

ENDLOOP.

endif.

CALL FUNCTION 'SCMS_STRING_TO_XSTRING'

EXPORTING

text = output

mimetype = 'APPLICATION/PDF'

IMPORTING

buffer = outputx.

concatenate cl_abap_char_utilities=>byte_order_mark_little

outputx

into outputx in byte mode.

response->set_header_field( name = 'content-type' value =

'APPLICATION/PDF; charset=utf-16le' ).

response->delete_header_field( name =

if_http_header_fields=>cache_control ).

response->delete_header_field( name = if_http_header_fields=>expires

).

response->delete_header_field( name = if_http_header_fields=>pragma )

.

response->set_header_field( name = 'content-disposition'

value = 'attachment; filename = TEST.PDF'

).

l_PDF_len = xstrlen( OUTPUTX ).

response->set_data( data = OUTPUTx

length = l_PDF_len ).

navigation->response_complete( ).

Regards

Amit Agrawal

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Have you checked out this weblog?

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

Former Member
0 Kudos

HI Craig Cmehil,

I have tried this code as well as given in the URL specified by you. But using this also browser shows the following content. It seems that PDF content is corrupted.

-

-


My Output as shown on Browser Starts below this line----

-


DF-1.3

%ÒãÏÓ

2 0 obj

/WinAnsiEncoding

endobj

3 0 obj

<<
/Type /Font
/Subtype /Type1
/BaseFont /Courier
/Name /F001
/Encodg 2 0 R
>>
endobj
4 0 obj
<<
/Length 5 0 R
>>
stream
/F001 7.30 Tf 0 0 0.753 rg BT 18.00 570.65 Td 0 Tw <33302E30362E3230302020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020>Tj ET 0 0 0.753 rg BT 2950 570.65 Td 0 Tw
<20204854545020436F6E74726F6C202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020>Tj ET 0 0 0.753 rg BT 572.40 570.65 Td 0 Tw <2020202020202020202020202031>Tj ET q 0 0 0 RG 0.55 w 18.00 562.90 m
634. 562.90 l S Q 0 0 0.753 rg BT 18.00 551.45 Td 0 Tw <48656C6C6F20416D6974>Tj ET 0 0 0.753 rg BT 18.00 541.85 Td 0 Tw <48656C6C6F204765726176>Tj ET

endstream

endobj

5 0 obj

624

endobj

6 0 obj

<<

/Type /Page

/MediaBox

/Parent 1 0 R

/Resource

<<

/ProcSet

/Font

<<

/F001 3 0 R

>>

/XObject

<<

>>

>>

/Contents 4 0 R

>>

endobj

7 0 obj

<<

/Author (AMG )

/CreationDate (20050630 181301)

/Creator (Form X_58_170 EN)

/Producer (SAP R/3 Release 620 )

%SAPinfoStart TOA_DARA

%FUNCTI=( )

%MANDANT=( )

%DEL_DATE=(00000000)

%SAP_OBJECT=( )

%AR_OBJECT=( )

%OBJECT_ID=( )

%FORM_ID=( )

%FORMARCHIV=( )

%RESERVE=( )

%NOTIZ=( )

%-( )

%-( )

%-( )

%SAPinfoEnd TOA_DARA

>>

endobj

1 0 obj

<<

/Type /Pages

/Kids

[ 6 0 R

]

/Count 1

>>

endobj

8 0 obj<<

/Type /Catalog

/Pages 1 0 R

/PageMode /UseNone

>>

endobj

xref

0 9

0000000000 65535 f

0000001801 00000 n

0000000017 0000n

0000000052 00000 n

0000000157 00000 n

0000000840 00000 n

0000000862 00000 n

0000001048 00000 n

0000001869 00000 n

trailer

/Size 9

/Root 8 0 R

/Info 7 0 R

>>

startxref

1944

%%EOF

-

-


Browser Displey Content Ends----

-


-

-


My Coding Starts----

-


Data: GG type String.

  • event handler for data retrieval

data : event type ref to cl_htmlb_event.

class cl_htmlb_manager definition load.

class CL_HTTP_RESPONSE DEFINITION LOAD.

DATA : PARAMS TYPE PRI_PARAMS,

VALID TYPE C,

GV_SUCCESS_SPOOL_NO LIKE SY-SPONO.

CALL FUNCTION 'GET_PRINT_PARAMETERS'

EXPORTING DESTINATION = 'LOCL'

  • COPIES = COUNT

  • LIST_NAME = 'TEST'

  • LIST_TEXT = 'Test NEW-PAGE PRINT ON'

IMMEDIATELY = ''

RELEASE = 'X'

NEW_LIST_ID = 'X'

  • EXPIRATION = DAYS

LINE_SIZE = 140

  • LINE_COUNT = 23

  • LAYOUT = 'X_PAPER'

  • SAP_COVER_PAGE = 'X'

  • RECEIVER = 'SAP*'

  • DEPARTMENT = 'System'

NO_DIALOG = 'X'

IMPORTING OUT_PARAMETERS = PARAMS

VALID = VALID.

IF VALID <> SPACE.

NEW-PAGE PRINT ON PARAMETERS PARAMS NO DIALOG.

write πŸ˜• 'Hello Amit'.

write πŸ˜• 'Hello Gaurav '.

NEW-PAGE PRINT OFF .

ENDIF.

  • GV_SUCCESS_SPOOL_NO = sy-spono.

data : spool_no TYPE TSP01-RQIDENT .

spool_no = sy-spono.

  • write: GV_SUCCESS_SPOOL_NO.

*write: spool_no.

  • IF display_type = 'convertspool'.

DATA: itab TYPE TLINE,

witab TYPE TABLE OF TLINE,

output TYPE STRING,

outputx TYPE XSTRING.

data : l_pdf_len type i.

data: mi_bytecount type i.

DATA : APP_TYPE TYPE STRING VALUE 'APPLICATION/PDF' .

DATA: cached_response TYPE ref to if_HTTP_RESPONSE.

data : l_display_url type string .

data : guid type GUID_32 .

*

  • spool_no like TSP01-RQIDENT .

*spool_no = GV_SUCCESS_SPOOL_NO .

call function 'CONVERT_ABAPSPOOLJOB_2_PDF'

EXPORTING

SRC_SPOOLID = spool_no

NO_DIALOG = 'X'

IMPORTING

PDF_BYTECOUNT = mi_bytecount

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.

if sy-subrc EQ 0.

LOOP AT witab INTO itab.

CONCATENATE

output

itab-tdline

INTO output in character mode.

ENDLOOP.

endif.

*write : output.

CALL FUNCTION 'SCMS_STRING_TO_XSTRING'

EXPORTING

text = output

mimetype = 'APPLICATION/PDF'

IMPORTING

buffer = outputx.

*concatenate cl_abap_char_utilities=>byte_order_mark_little

*outputx

*into outputx in byte mode.

CREATE OBJECT cached_response TYPE CL_HTTP_RESPONSE EXPORTING add_c_msg = 1.

  • some Browsers have caching problems when loading Excel Format

response->set_header_field( name = 'content-type' value =

'APPLICATION/PDF' ).

response->delete_header_field( name =

if_http_header_fields=>cache_control ).

response->delete_header_field( name = if_http_header_fields=>expires

).

response->delete_header_field( name = if_http_header_fields=>pragma )

.

l_pdf_len = xstrlen( outputx ).

cached_response->set_data( data = outputx

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

cl_http_server=>server_cache_upload( url = l_display_url

response = cached_response ).

RETURN.

-

-


My Coding Ends----

-


Any Further help on this.

Regards

Amit Agrawal

Former Member
0 Kudos

It looks at first glace I haven't tried your code but it looks like your browser can't render PDF files what version of brwoser, what version of Acrobat reader and what version and SP level of system do you have?

Former Member
0 Kudos

Hi Craig,

The Browser which we are using is

Internet Explorer 6.0.2600.0000,

Acrobat Reader is version 5.

SAP_BASIS SP is SAPKB62041( I hope you have asked for SAP basis service pack version, if not so plese let me know)

However even if I do not open The PDF in browser and instead save it on the hard disk of my machine ( see the original code in my first message) then also the content of PDF file if seen on a notepad are exactly same and corrupted. This might indicate that PDF gets corrupt even before rendering it on the Browser, but not sure. Can there be some problem inside the 'CONVERT_ABAPSPOOLJOB_2_PDF'method or may be some problem in the lenght of the string as "P" of PDF is not appearing in the first line of PDF content ( see the PDF content as it appears in a Browser or when opened on a notepad) .

Regards

Amit Agrawal

Message was edited by: Amit Agrawal

Former Member
0 Kudos

What happens when you open the PDF with Acrobat Reader?

Have you tried simply the exact example code from the weblog? Your code looks a bit different code just be formatting here in the forum though.

Former Member
0 Kudos

Hi Craig ,

I am copying the exact code as taken from your weblog , this code first submits a simple report to spool and take spool no and pass it to function module 'CONVERT_ABAPSPOOLJOB_2_PDF' which return a table, PDF STRUCTURE TLINE, convert this PDFtable in to XSTRING , then after creating a GUID , create a URL . My problem is that this URL does n't show PDF file.,although when I debugg this code , I can see my spool is getting generated and that spool has a simple report as required and I can see my PDF file table as well.I can see the URL also in debugg mode not sure weather its right or not.

I am putting the code here.

************************************

data : event type ref to cl_htmlb_event.

DATA: cached_response TYPE REF TO if_http_response.

DATA: guid TYPE guid_32.

class CL_HTTP_RESPONSE DEFINITION LOAD.

DATA : PARAMS TYPE PRI_PARAMS,

VALID TYPE C.

data: l_pdf_xstring type xstring,

lt_lines type table of tline,

ls_line type tline,

l_pdf_len type i.

CALL FUNCTION 'GET_PRINT_PARAMETERS'

EXPORTING DESTINATION = 'LOCL'

  • COPIES = COUNT

  • LIST_NAME = 'TEST'

  • LIST_TEXT = 'Test NEW-PAGE PRINT ON'

IMMEDIATELY = ''

RELEASE = 'X'

NEW_LIST_ID = 'X'

  • EXPIRATION = DAYS

LINE_SIZE = 140

  • LINE_COUNT = 23

  • LAYOUT = 'X_PAPER'

  • SAP_COVER_PAGE = 'X'

  • RECEIVER = 'SAP*'

  • DEPARTMENT = 'System'

NO_DIALOG = 'X'

IMPORTING OUT_PARAMETERS = PARAMS

VALID = VALID.

IF VALID <> SPACE.

NEW-PAGE PRINT ON PARAMETERS PARAMS NO DIALOG.

write πŸ˜• 'Hello Amit'.

write πŸ˜• 'Hello Gaurav '.

NEW-PAGE PRINT OFF .

ENDIF.

data : spool_no TYPE TSP01-RQIDENT .

spool_no = sy-spono.

*IF display_type = 'convertspool'.

DATA: itab TYPE TLINE,

witab TYPE TABLE OF TLINE,

output TYPE STRING,

outputx TYPE XSTRING.

call function 'CONVERT_ABAPSPOOLJOB_2_PDF'

EXPORTING

SRC_SPOOLID = spool_no

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.

if sy-subrc EQ 0.

LOOP AT witab INTO itab.

CONCATENATE

output

itab-tdline

INTO output.

ENDLOOP.

****Convert the Character String to Binary String

data: r_string type string.

data: R_XSTRING type xstring.

call function 'SCMS_STRING_TO_XSTRING'

EXPORTING

text = output

IMPORTING

buffer = outputx.

CREATE OBJECT cached_response TYPE CL_HTTP_RESPONSE EXPORTING add_c_msg = 1.

l_pdf_len = xstrlen( outputx ).

cached_response->set_data( data = outputx

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

ENDIF.

RETURN.

*ENDIF.

*************************

One more thign in your weblog you have mentioned that we can save our PDF table with .pdf extension , how do we do that ??

Thanks

Amit

Former Member
0 Kudos

Are you sure it says table and not file?

the display_url when you debug it what value does it have?

Former Member
0 Kudos

Hi Craig,

To quote from Weblog : "sy-spono will then contain your Spool number which you can then use with the function module CONVERT_ABAPSPOOLJOB_2_PDF with the Spool number as a parameter. This will return a table, PDF STRUCTURE TLINE, which you can then save with .pdf extension" .

I wanted to know how do we save this table with .pdf extension.

and follwoing is the display_url I get in debug mode.

/sap(bD1lbiZjPTIxMA==)/bc/bsp/sap/zbsp_pdf_4/9D62899BF4B11C4CBC31A07F61A56D46.pdf

Thanks

Amit

Former Member
0 Kudos

See this link for a better explaination of that part of the Weblog, Raja, contributed that part - the weblog was a group effort.

That link should clear up that statement.

I just tried the code from the weblog and it worked ok for me. I have to get going but if I can a chance I'll try to compare the two code segements and see what I can see differnt.

Former Member
0 Kudos

Dear Craig Cmehil,

I was able to generate the PDF with your code with very minor modification for taking care of blank spaces as suggested by Raja.

Thank you very much for your help. Please also let me know if there is a way by which I can convert a HTML ( not an OTF string) string to PDF output without using a third party software. Does SAP provide any functions for this?

Regards

Amit Agrawal

Former Member
0 Kudos

Good question, but to be honest I'm not sure and I've not even tried it.

But you should be able to do a quick test by reading in the HTML (see my weblog <a href="/people/sap.user72/blog/2005/05/03/sdn-meets-labs-integrated-web-content Web Content</a>) and store the content and then translate it - perhaps the spool method would be best.

maximilian_schaufler
Active Contributor
0 Kudos

Hi Amit,

did you do any further testing on this?

If so, please let us know about it, I (and others as well) would be very interested in your results.

Max

Answers (0)