cancel
Showing results for 
Search instead for 
Did you mean: 

Smartform : PDF to Base64 encoding

Former Member
0 Kudos

Hi .

I am new to ABAP & Smartform.I have a requirement to convert smartform PDF to Base 64.I've tried the methods provided in the related SDN threads.But while decoding , junk characters are coming.Please find my code below :

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

    • Convert SF output to PDF format

*convert the smartform to .pdf file

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

DATA : I_DOC TYPE TABLE OF DOCS,

WA_DOC TYPE DOCS,

L_PDF_STRING TYPE XSTRING.

I_OTFDATA[] = ST_JOB_OUTPUT_INFO-OTFDATA[].

CALL FUNCTION 'CONVERT_OTF'

EXPORTING

FORMAT = 'PDF'

MAX_LINEWIDTH = 132

IMPORTING

BIN_FILESIZE = V_BIN_FILESIZE

BIN_FILE = L_PDF_STRING

TABLES

OTF = I_OTFDATA

LINES = I_LINES.

  • Convert Table i_bin to base64 encoding

DATA : I_BASE64 TYPE STANDARD TABLE OF SOLISTI1,

WA_BASE64 TYPE SOLISTI1,

WA_LINES TYPE TLINE.

DATA: LV_OUTPUT(255) TYPE C,

LV_INPUT(132) TYPE C,

LV_X(255) TYPE X,

LV_I TYPE I.

DATA: CONV TYPE REF TO CL_ABAP_CONV_OUT_CE.

LOOP AT I_LINES INTO WA_LINES.

LV_INPUT = WA_LINES-TDLINE.

LV_I = STRLEN( LV_INPUT ).

  • CONDENSE lv_input.

IF LV_I > 0.

TRY.

CALL METHOD CL_ABAP_CONV_OUT_CE=>CREATE

EXPORTING

ENCODING = 'UTF-8'

ENDIAN = 'L'

  • replacement = '#'

  • ignore_cerr = ABAP_FALSE

RECEIVING

CONV = CONV .

CATCH CX_PARAMETER_INVALID_RANGE .

CATCH CX_SY_CODEPAGE_CONVERTER_INIT .

ENDTRY.

CALL METHOD CONV->WRITE

EXPORTING

N = LV_I

DATA = LV_INPUT

  • view =

  • IMPORTING

  • len =

.

LV_X = CONV->GET_BUFFER( ).

CALL FUNCTION 'HTTP_BASE64_ENCODE'

EXPORTING

INPUT = LV_X

INPUT_LENGTH = LV_I

IMPORTING

OUTPUT = LV_OUTPUT.

  • Append to Table

WA_BASE64-LINE = LV_OUTPUT.

APPEND WA_BASE64 TO I_BASE64.

CLEAR : LV_OUTPUT, LV_X, LV_I, WA_BASE64, WA_LINES.

ENDIF.

ENDLOOP.

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

Thanks,

Jayita Ganguly

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hello

Please refer to the following SAP note regarding conversion - 999712 - PDF conversion for Unicode

Regards.

Answers (1)

Answers (1)

Sandra_Rossi
Active Contributor
0 Kudos

Hi Jayita Ganguly,

I have a requirement to convert smartform PDF to Base 64.I've tried the methods provided in the related SDN threads.But while decoding , junk characters are coming

If you encode and decode, then you revert back to the original PDF, which is a binary stream. So it's normal you get weird characters if you try to display it as characters! But maybe you didn't give enough information about where you got the junk characters!?

Sandra

Former Member
0 Kudos

Thanks Sandra ,

For decoding we are using the following link where binary file is created but unable to open that in PDF.

http://www.opinionatedgeek.com/dotnet/tools/Base64Decode/Default.aspx

Thanks,

Jayita

Sandra_Rossi
Active Contributor
0 Kudos

It's normal that you get weird characters if you try to display the PDF binary stream as characters! Open a PDF file with Notepad and you'll see the same result!

Former Member
0 Kudos

Thanks...Can you let me know that the code and the function modules I'm using are correct or not?

Sandra_Rossi
Active Contributor
0 Kudos

I see a big error in your code : you MUST NOT apply character encoding conversion on a binary stream !

But why don't you say that the PDF Reader can't open the PDF file, instead of assuming there is a base64 error... Just give the facts, only the facts.

Sandra

Former Member
0 Kudos

Hi,

I have removed the encoding but the Output is Null only series of AAAAAAAAAAAAAAAAAAAA coming in BASE 64 conversion.

Please find below my new code ..

It would be helpful if you can provide me the exact code for conversion.

LOOP AT I_PDF to WA_PDF .

LV_X = WA_PDF-TDLINE .

CALL FUNCTION 'HTTP_BASE64_ENCODE'

EXPORTING

INPUT = LV_X

INPUT_LENGTH = LV_I

IMPORTING

OUTPUT = LV_OUTPUT.

Write : / LV_OUTPUT.

ENDLOOP.

Sandra_Rossi
Active Contributor
0 Kudos

Hi,

LV_X = WA_PDF-TDLINE.

As WA_PDF-TDLINE is type C, and LV_X is type X, it does a conversion BUT it is not what you expect: LV_X = 'RMjlùIOJM%8RUZ3T', it will set LV_X = all zeroes as 'RM...' does not start with an hexadecimal string (and so base 64 would return AAAAAA). See the ABAP doc for more info.

Moreover, base 64 encoding must be done with the whole byte stream (more precisely you must not split it outside chunks of 3 bytes, as this encoding converts 3 bytes into 4 characters, converting 2 bytes makes no sense for example, except at the end of the stream).

I advise you to use CL_HTTP_UTILITY=>ENCODE_BASE64 which converts a string which is casted as bytes. Before, you must concatenate the lines into a string, either using CONCATENATE LINES OF lt_char INTO l_target RESPECTING BLANKS, or SWA_STRING_FROM_TABLE (keep_trailing_spaces='X') depending on your release. Don't forget to shorten the string at exactly the number of representative characters (LV_I in your example).

BR

Sandra