You are on page 1of 24

How to Print(PDF) QR Codes in standard SAP

QR Code SAP Device Type using Barcode Writer in Pure Postscript



Ever since coming across a QR code in my passport I have wanted to integrate them into SAP. I like the idea of a
"quick response" of data into a mobile device as I hate typing on mobile phone keyboards. Now this is a technical
solution and no business case is provided, however the flight demo SAP sapscript and smartforms which will be
covered later offer an interesting use of QR codes (in my opinion).

Still unsure about QR codes, then try the Wikipedia link
See them in action here with Google's favourite places advert

I am more of an system integrator/administrator than a developer so I found,
*) the application to do the actual QR code generation, Terry Burton's Barcode Writer in Pure Postscript, here
*) For simplicity I use the Cute PDF writer to generate the PDF via SAPlpd. A mention must go out to Ghostscript as
this is used to do the actual QR Code PDF conversion. So this needs to be installed as per the instructions for Cute
PDF writer.
*) Configure ABAP list program/SAPSCRIPT/SMARTFORMS to use the device type printer controls to generate the
QR Codes.

Here is how to generate QR codes in SAP.

1) SPAD - Import device type FILE=ZBWIPPQR.PRI
You can download the ZBWIPPQR device type here

On the next screen enter ZBWIPPQR as the device type name



The ZBWIPPQR.PRI is a plain text file which you can open with any text editor to check it out.

2) Import output device FILE = zbwipp.txt
Download text file ZBWIPP.TXT for import here


next screen enter ZBWIPP.txt as the file name (use the directory where you downloaded the file)


3) Install Cute PDF writer from here
(direct download here http://www.cutepdf.com/download/CuteWriter.exe )
It is important to install with the PS2PDF option, so answer yes at the following prompt.



RENAME Cute PDF Writer printer to qrcodes as shown here.
Before


After



Why rename the printer to qrcodes?
This must be renamed to qrcodes to match the imported output device as shown below in the SPAD output screen.
(SAPLPD will now use the printer qrcodes, in this way it does NOT interfere with any other SAP frontend printer
settings you may have).



5) To enable frontend printing please ensure the user settings for spool control are set to "output immediately" in
SU01.

Defaults TAB screen shot of the setting required. (make sure it is set, log off and check it again :)




6) Import the following example ABAP code to test QR codes in SAP.
From transaction SE38 create ABAP report ZQR_PRINT.
Cut and paste the text from this file found here ZQR_PRINT

Ignore any warning only messages relating to NEW-PAGE PRINT ON, this is purely used as a simple method to allow
the ABAP code to use the printer controls in the device type.

Activate the program as the message is only a warning. This is only a test for ABAP list prints.


Now run the ZQR_PRINT report

Select ZBWIPP as the output device in the following print parameter screen.

*please note that the options shown here purely depend on the options you have active in the user settings. So its
important, as previously stated, to have already set print immediately as one of the options.



After selecting the green tick mark, then after a short while you should have a prompt to save a PDF file.


7) The actual QR Code PDF document should look something like this....



8)... to be continued and the simple next step to use the printer controls in SAPSCRIPT and SMARTFORMS.
The YouTube videos for smartforms and sapscript now below....
*the video also shows other barcode examples.
1) The DataMatrix device type can be downloaded here
2) The Aztec Code device type can be downloaded here
*Not shown in the video but also available
3) The MaxiCode device type can be downloaded here











Create a smartform. Follow the steps given below.




2. Go to Form Interface and add a new variable under the import tab which holds the name
of the QR code that we are passing from the driver program [W_NAME type char20]



3. Create a new graphics and as shown below give the name as &W_NAME&. [Dynamic
variable]




4. Done.
5. Copy the code below.
6. Enjoy the emerging technology of QR code in SAP.


Note: In the program given below I have used a screen 9000. Please do create it before running and also create a custom
control in the screen layout and give the name PICTURECONTROL to it.

SOURCE CODE



*&---------------------------------------------------------------------*
*& Report Z03_QRCODE_IMAGE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*



REPORT z03_qrcode_image.

*types: ty_boolean(1) type c.
*
*constants:
* c_true type ty_boolean value 'X',
* c_false type ty_boolean value space.

DATA : bds_description like bapisignat-prop_value.


* BDS handling
constants:
c_bds_classname type sbdst_classname value 'DEVC_STXD_BITMAP',
c_bds_classtype type sbdst_classtype value 'OT', " others
c_bds_mimetype type bds_mimetp value 'application/octet-stream',
c_bds_original type sbdst_doc_var_tg value 'OR'.



* Graphic handling
constants:
c_stdtext like thead-tdobject value 'TEXT',
c_graphics like thead-tdobject value 'GRAPHICS',
c_bmon like thead-tdid value 'BMON',
c_bcol like thead-tdid value 'BCOL'.


DATA: gi_filename type rlgrap-filename,
gi_name type stxbitmaps-tdname,
gi_object type stxbitmaps-tdobject,
gi_id type stxbitmaps-tdid,
gi_btype type stxbitmaps-tdbtype,
gi_resident type stxbitmaps-resident,
gi_autoheight type stxbitmaps-autoheight,
gi_bmcomp type stxbitmaps-bmcomp,
gi_resolution type stxbitmaps-resolution,
l_extension type rlgrap-filename,
l_docid type stxbitmaps-docid.

"Picture Control
DATA: picture_container TYPE REF TO cl_gui_custom_container,
picture_control TYPE REF TO cl_gui_picture.


DATA: l_img_url TYPE w3url.
DATA :l_img_subtype TYPE w3param-cont_type.
DATA : l_str_length TYPE i.
DATA : url TYPE string.
DATA : l_content_length TYPE i.

DATA : mime TYPE w3mimetabtype.
DATA: blob TYPE w3mimetabtype,
blob_size TYPE w3param-cont_len,
blob_type TYPE w3param-cont_type.

DATA : i_igs_image_converter TYPE REF TO cl_igs_image_converter.
DATA: content TYPE xstring.
DATA : http_client TYPE REF TO if_http_client.


TYPES : BEGIN OF ty_binary,
binary_field(1000) TYPE c,
END OF ty_binary.

DATA : hex_tab1 TYPE TABLE OF ty_binary WITH HEADER LINE.


SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS : qr_text TYPE char100 OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.



SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-002.
PARAMETERS : width TYPE int3,
height TYPE int3.
SELECTION-SCREEN END OF BLOCK b2.



PARAMETERS : p_rad1 RADIOBUTTON GROUP rd1 DEFAULT 'X',
p_rad2 RADIOBUTTON GROUP rd1.


START-OF-SELECTION.

PERFORM download_qrcode.
PERFORM convert_image.

*&---------------------------------------------------------------------*
*& Form DOWNLOAD_QRCODE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*

FORM download_qrcode .

CONCATENATE 'http://chart.apis.google.com/chart?chs=200x200&cht=qr&chld=|1&chl='qr_text '/chart.png' INTO url.

CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = url
IMPORTING
client = http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.


IF sy-subrc = 0.

http_client->send( ).

http_client->receive( ).

content = http_client->response->get_data( ).

http_client->close( ).

l_str_length = xstrlen( content ).

CALL FUNCTION 'RSFO_XSTRING_TO_MIME'
EXPORTING
c_xstring = content
i_length = l_str_length
TABLES
c_t_mime = mime.

ENDIF.

ENDFORM. " DOWNLOAD_QRCODE

*&---------------------------------------------------------------------*
*& Form CONVERT_IMAGE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*

FORM convert_image .

CREATE OBJECT i_igs_image_converter .

i_igs_image_converter->input = 'image/png'.
i_igs_image_converter->output = 'image/bmp'.
i_igs_image_converter->width = width.
i_igs_image_converter->height = height.

CALL METHOD i_igs_image_converter->set_image
EXPORTING
blob = mime
blob_size = l_content_length.


CALL METHOD i_igs_image_converter->execute
EXCEPTIONS
communication_error = 1
internal_error = 2
external_error = 3
OTHERS = 4.

IF sy-subrc = 0.

CALL METHOD i_igs_image_converter->get_image
IMPORTING
blob = blob
blob_size = blob_size
blob_type = blob_type.

ENDIF.

IF sy-subrc = 0.

IF p_rad1 = 'X'.

CALL SCREEN '9000'. "Calling the screen for qrcode display

ELSE.

PERFORM show_smart_form. "calling the smartform for qrcode display

ENDIF.

ENDIF.

ENDFORM. " CONVERT_IMAGE


*&---------------------------------------------------------------------*
*& Module STATUS_9000 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*

MODULE status_9000 OUTPUT.

SET PF-STATUS 'PF'.

"Creating the object for the container

CREATE OBJECT picture_container
EXPORTING
container_name = 'PICTURECONTROL'.
CREATE OBJECT picture_control
EXPORTING
parent = picture_container.

"Calling the screen

PERFORM call_screen.

ENDMODULE. " STATUS_9000 OUTPUT

*&---------------------------------------------------------------------*
*& Form CALL_SCREEN
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*

FORM call_screen .

"Creating the url of the image for the display in the container in the screen
SPLIT blob_type AT '/' INTO blob_type l_img_subtype.

CALL FUNCTION 'DP_CREATE_URL'
EXPORTING
type = blob_type
subtype = l_img_subtype
size = blob_size
lifetime = cndp_lifetime_transaction
TABLES
data = blob
CHANGING
url = l_img_url
EXCEPTIONS
OTHERS = 1.


IF sy-subrc IS INITIAL.
CALL METHOD picture_control->load_picture_from_url
EXPORTING
url = l_img_url.
ENDIF.

ENDFORM. " CALL_SCREEN

*&---------------------------------------------------------------------*
*& Module USER_COMMAND_9000 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE user_command_9000 INPUT.

IF sy-ucomm = 'BACK'.

LEAVE TO SCREEN 0.

ENDIF.

ENDMODULE. " USER_COMMAND_9000 INPUT

*&---------------------------------------------------------------------*
*& Form SHOW_SMART_FORM
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*

FORM show_smart_form .

gi_name = 'QRCODE10'. "name of the qrcode will be in se78 after one time running this program
gi_object = 'GRAPHICS'.
gi_id = 'BMAP'.
gi_btype = 'BCOL'. "If u want black and white pass BMON
gi_resident = ' '.
gi_autoheight = 'X'.
gi_bmcomp = 'X'.
l_extension = 'BMP'.

"importing the image into se78 before displaying it in the smartform.

perform import_bitmap_bds using blob
gi_name
gi_object
gi_id
gi_btype
l_extension
' '
gi_resident
gi_autoheight
gi_bmcomp
changing l_docid
gi_resolution.


IF sy-subrc = 0.

DATA:fname TYPE rs38l_fnam.

"gettingt the name FM of the smartform
CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
EXPORTING
formname = 'ZQR_TEST'
* VARIANT = ' '
* DIRECT_CALL = ' '
IMPORTING
fm_name = fname
* EXCEPTIONS
* NO_FORM = 1
* NO_FUNCTION_MODULE = 2
* OTHERS = 3 .

"Calling the FM of the smartform for display
CALL FUNCTION fname
EXPORTING
* ARCHIVE_INDEX =
* ARCHIVE_INDEX_TAB =
* ARCHIVE_PARAMETERS =
* CONTROL_PARAMETERS =
* MAIL_APPL_OBJ =
* MAIL_RECIPIENT =
* MAIL_SENDER =
* OUTPUT_OPTIONS =
* USER_SETTINGS = 'X'
w_name = 'QRCODE10'
* IMPORTING
* DOCUMENT_OUTPUT_INFO =
* JOB_OUTPUT_INFO =
* JOB_OUTPUT_OPTIONS =
* EXCEPTIONS
* FORMATTING_ERROR = 1
* INTERNAL_ERROR = 2
* SEND_ERROR = 3
* USER_CANCELED = 4
* OTHERS = 5 .

IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

ENDIF.

ENDFORM. " SHOW_SMART_FORM

*&---------------------------------------------------------------------*
*& Form IMPORT_BITMAP_BDS (Copied from standard program and modified it as per the requirement)
*&---------------------------------------------------------------------*
form import_bitmap_bds
using p_blob type w3mimetabtype
p_name type stxbitmaps-tdname
p_object type stxbitmaps-tdobject
p_id type stxbitmaps-tdid
p_btype type stxbitmaps-tdbtype
p_format type c
p_title like bds_description
p_resident type stxbitmaps-resident
p_autoheight type stxbitmaps-autoheight
p_bmcomp type stxbitmaps-bmcomp
changing p_docid type stxbitmaps-docid
p_resolution type stxbitmaps-resolution.


data: l_object_key type sbdst_object_key.
data: l_tab type ddobjname.

data: begin of l_bitmap occurs 0,
l(64) type x,
end of l_bitmap.

data: l_filename type string,
l_bytecount type i,
l_bds_bytecount type i.
data: l_color(1) type c,

l_width_tw type stxbitmaps-widthtw,
l_height_tw type stxbitmaps-heighttw,
l_width_pix type stxbitmaps-widthpix,
l_height_pix type stxbitmaps-heightpix.
data: l_bds_object type ref to cl_bds_document_set,
l_bds_content type sbdst_content,
l_bds_components type sbdst_components,
wa_bds_components type line of sbdst_components,
l_bds_signature type sbdst_signature,
wa_bds_signature type line of sbdst_signature,
l_bds_properties type sbdst_properties,
wa_bds_properties type line of sbdst_properties.

data wa_stxbitmaps type stxbitmaps.



* Enqueue
perform enqueue_graphic using p_object
p_name
p_id
p_btype.


* Bitmap conversion
call function 'SAPSCRIPT_CONVERT_BITMAP_BDS'
exporting
color = 'X'
format = p_format
resident = p_resident
bitmap_bytecount = l_bytecount
compress_bitmap = p_bmcomp
importing
width_tw = l_width_tw
height_tw = l_height_tw
width_pix = l_width_pix
height_pix = l_height_pix
dpi = p_resolution
bds_bytecount = l_bds_bytecount
tables
bitmap_file = p_blob
bitmap_file_bds = l_bds_content
exceptions
format_not_supported = 1
no_bmp_file = 2
bmperr_invalid_format = 3
bmperr_no_colortable = 4
bmperr_unsup_compression = 5
bmperr_corrupt_rle_data = 6
others = 7.

if sy-subrc <> 0.

perform dequeue_graphic using p_object
p_name
p_id
p_btype.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
raising conversion_failed.

endif.



* Save bitmap in BDS
create object l_bds_object.

wa_bds_components-doc_count = '1'.
wa_bds_components-comp_count = '1'.
wa_bds_components-mimetype = c_bds_mimetype.
wa_bds_components-comp_size = l_bds_bytecount.
append wa_bds_components to l_bds_components.

if p_docid is initial. " graphic is new

wa_bds_signature-doc_count = '1'.
append wa_bds_signature to l_bds_signature.


call method l_bds_object->create_with_table
exporting
classname = c_bds_classname
classtype = c_bds_classtype
components = l_bds_components
content = l_bds_content
changing
signature = l_bds_signature
object_key = l_object_key
exceptions
others = 1.

if sy-subrc <> 0.

perform dequeue_graphic using p_object
p_name
p_id
p_btype.
* message e285 with p_name 'BDS'.

endif.

read table l_bds_signature index 1 into wa_bds_signature
transporting doc_id.

if sy-subrc = 0.

p_docid = wa_bds_signature-doc_id.

else.

perform dequeue_graphic using p_object
p_name
p_id
p_btype.
* message e285 with p_name 'BDS'.

endif.

else. " graphic already exists

********* read object_key for faster access *****
clear l_object_key.
select single * from stxbitmaps into wa_stxbitmaps
where tdobject = p_object
and tdid = p_id
and tdname = p_name
and tdbtype = p_btype.

select single tabname from bds_locl into l_tab
where classname = c_bds_classname
and classtype = c_bds_classtype.

if sy-subrc = 0.

select single object_key from (l_tab) into l_object_key
where loio_id = wa_stxbitmaps-docid+10(32)
and classname = c_bds_classname
and classtype = c_bds_classtype.

endif.

******** read object_key end ********************

call method l_bds_object->update_with_table
exporting
classname = c_bds_classname
classtype = c_bds_classtype
object_key = l_object_key
doc_id = p_docid
doc_ver_no = '1'
doc_var_id = '1'
changing
components = l_bds_components
content = l_bds_content
exceptions
nothing_found = 1
others = 2.

if sy-subrc = 1. " inconsistency STXBITMAPS - BDS; repeat check in

wa_bds_signature-doc_count = '1'.
append wa_bds_signature to l_bds_signature.

call method l_bds_object->create_with_table
exporting
classname = c_bds_classname
classtype = c_bds_classtype
components = l_bds_components
content = l_bds_content
changing
signature = l_bds_signature
object_key = l_object_key
exceptions
others = 1.

if sy-subrc <> 0.
perform dequeue_graphic using p_object
p_name
p_id
p_btype.
* message e285 with p_name 'BDS'.

endif.

read table l_bds_signature index 1 into wa_bds_signature
transporting doc_id.
if sy-subrc = 0.
p_docid = wa_bds_signature-doc_id.
else.

perform dequeue_graphic using p_object
p_name
p_id
p_btype.

* message e285 with p_name 'BDS'.

endif.

elseif sy-subrc = 2.

perform dequeue_graphic using p_object
p_name
p_id
p_btype.

* message e285 with p_name 'BDS'.

endif.

endif.

* Save bitmap header in STXBITPMAPS
wa_stxbitmaps-tdname = p_name.
wa_stxbitmaps-tdobject = p_object.
wa_stxbitmaps-tdid = p_id.
wa_stxbitmaps-tdbtype = p_btype.
wa_stxbitmaps-docid = p_docid.
wa_stxbitmaps-widthpix = l_width_pix.
wa_stxbitmaps-heightpix = l_height_pix.
wa_stxbitmaps-widthtw = l_width_tw.
wa_stxbitmaps-heighttw = l_height_tw.
wa_stxbitmaps-resolution = p_resolution.
wa_stxbitmaps-resident = p_resident.
wa_stxbitmaps-autoheight = p_autoheight.
wa_stxbitmaps-bmcomp = p_bmcomp.
insert into stxbitmaps values wa_stxbitmaps.

if sy-subrc <> 0.

update stxbitmaps from wa_stxbitmaps.

if sy-subrc <> 0.

* message e285 with p_name 'STXBITMAPS'.

endif.

endif.



* Set description in BDS attributes

wa_bds_properties-prop_name = 'DESCRIPTION'.
wa_bds_properties-prop_value = p_title.
append wa_bds_properties to l_bds_properties.


call method l_bds_object->change_properties
exporting
classname = c_bds_classname
classtype = c_bds_classtype
object_key = l_object_key
doc_id = p_docid
doc_ver_no = '1'
doc_var_id = '1'
changing
properties = l_bds_properties
exceptions
others = 1.

perform dequeue_graphic using p_object
p_name
p_id
p_btype.



endform.

*&---------------------------------------------------------------------*
*& Form ENQUEUE_GRAPHIC
*&---------------------------------------------------------------------*
* Enqueue of graphics stored in BDS
*----------------------------------------------------------------------*

form enqueue_graphic using p_object
p_name
p_id
p_btype.


call function 'ENQUEUE_ESSGRABDS'
exporting
* MODE_STXBITMAPS = 'E'
tdobject = p_object
tdname = p_name
tdid = p_id
tdbtype = p_btype
* X_TDOBJECT = ' '
* X_TDNAME = ' '
* X_TDID = ' '
* X_TDBTYPE = ' '
* _SCOPE = '2'
* _WAIT = ' '
* _COLLECT = ' '
exceptions
foreign_lock = 1
others = 2.

if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
raising enqueue_failed.
endif.

endform. " ENQUEUE_GRAPHIC



*&---------------------------------------------------------------------*
*& Form DEQUEUE_GRAPHIC
*&---------------------------------------------------------------------*
* Dequeue of graphics stored in BDS
*----------------------------------------------------------------------*

form dequeue_graphic using p_object
p_name
p_id
p_btype.

call function 'DEQUEUE_ESSGRABDS'
exporting
* MODE_STXBITMAPS = 'E'
* X_TDOBJECT = ' '
* X_TDNAME = ' '
* X_TDID = ' '
* X_TDBTYPE = ' '
* _SCOPE = '3'
* _SYNCHRON = ' '
* _COLLECT = ' '
tdobject = p_object
tdname = p_name
tdid = p_id
tdbtype = p_btype.

endform. " DEQUEUE_GRAPHIC



OUTPUT