To upload a PDF binary file to SAP I can use an RFC function module. This FM requires two specific parameters:
IC_ARC_OBJECT
= the document class to define the mime type in SAP (OAC2)IN_CONTENT_LENGHT
= IMPORTING data type, is the size of the fileIT_CONTENT
= IMPORTING table of structureTBL1024
, table with the bytes of the file, cut into pieces of size 1024ET_MESSAGES
= just aBAPIRET2
table to check on errors (RETURN table)
The function module is successfully executed in the Python program but when I break on the function module to check on the content of IT_CONTENT
I can see that the lines of the table are not filled in correctly:
Extraction of the python program call of the FM:
arc_object = "ZZ_PDF"
content = Arc.get_arc_content(filename)
size = Arc.get_size(filename)
messages = []
result = connection.call("ZZ_UPLOAD_FILE",
IC_ARC_OBJECT = arc_object,
IN_CONTENT_LENGTH = str(size),
IT_CONTENT = content,
ET_MESSAGES = messages)
messages = result['ET_MESSAGES']
for mes in messages:
print mes['MESSAGE']
Extraction of the python function Arc.get_arc_content(filename)
:
def get_arc_content(filename="file.pdf"):
maxLineLength = 1024
structure = []
if filename == '':
filename = 'file.pdf'
with open(filename, 'rb') as file1:
f = file1.read()
n = 1024
fl = [f[i:i + n] for i in range(0, len(f), n)]
content = []
for line in fl:
lineapp = {
u"LINE": line
}
print len(line)
content.append(lineapp)
return content
The result in SAP is the following for the parameters:
IN_CONTENT_LENGHT
= 000000007945 (7.8 kb)IT_CONTENT
= 8 x 1 lines: (4lines)
LINE1 255044462D312E3300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 LINE2 B89B067867673EAB00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 LINE3 5BF4CB23B9A7513700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 LINE4 61DB44B3B0AAB12500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 .... LINE8 202020202020202000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
The expecting result for IT_CONTENT
, checked with FM ARCHIVE_OBJECT_GET
, should be something like:
LINE1 255044462D312E330D0A25E2E3CFD30D0A25525354585044463320506172616D65746572733A204452535458680D0A322030206F626A0D0A3C3C0D0A2F46696C7465722033203020520D0A2F4C656E67 LINE2 FA1D555450C8EF41AC263F14BC03C49BDD773CF22BC43FD21B3565DA2DFCCB83D8FFAC7BCBBBEBE6DED0AD68DEA29B349398511824DDDCFFAF41EA7FDADDF67EE61EF3FC303293F7E96351A911F8DE10 LINE3 FB4A559526CD6AED428976E80F8DD0B02517C4178D1EFB73C65792D09CFE5C717883BE38E61ED0AB3ADC0983C103E4338A1B210DA086DA73375E679C0A410F510FDC542BB686A1A4D601DD411A506504 ...
So the problem is that only the first bytes are transferred, the rest are trailing zero's.
Is this related to the fact that I'm doing something wrong, or isn't it possible to cut the binary files properly into chunks of 1024 and transfer them into a table towards SAP. How to use the byte type of SAP in python?
Many thanks for your suggestions/support.
IN_CONTENT_LENGHT
parameter set properly by you? – Suncatcherdef get_size(filename="file.pdf"): if filename == '': filename = 'file.pdf' print file_size(filename) stats = os.stat(filename) size = stats.st_size return size
– N. Leirs