How to get “PDF” file from the binary data of Soft

2019-09-24 16:00发布

I got the binary data by "getPdf" method of SoftLayer's API.

Ref. BillingSoftLayer_Billing_Order_Quote::getPdf | SoftLayer Development Network - http://sldn.softlayer.com/reference/services/SoftLayer_Billing_Order_Quote/getPdf

Then I wanna create the PDF file from the binary data. Do you know how to proceed it?

2条回答
SAY GOODBYE
2楼-- · 2019-09-24 16:01

the method return a binary data encoded in base 64, what you need to do is decode the binary data.

see this article about enconde and decode binary data.

https://code.tutsplus.com/tutorials/base64-encoding-and-decoding-using-python--cms-25588

the Python client returns a xmlrpc.client.Binary object so you need to work with that object here an example using the Python client and Python 3

#!/usr/bin/env python

import SoftLayer
import xmlrpc.client
import base64
import os

USERNAME = 'set me'
API_KEY = 'set me'

quoteId = 1560845

client = SoftLayer.Client(username=USERNAME, api_key=API_KEY)

accountClient = client['SoftLayer_Billing_Order_Quote']
binaryData = accountClient.getPdf(id=quoteId)
decodeBinary = binaryData.data
file = open('test.pdf','wb')
file.write(decodeBinary)

Regards

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-09-24 16:20

This is my answer fo my question.

# import
import SoftLayer
import sys
parm=sys.argv
quoteId=parm[1]

# account info
client = SoftLayer.create_client_from_env()

# getPdf as a binary data
getPdf = client['Billing_Order_Quote'].getPdf(id=quoteId)

# Save as a PDF
quoteFileName = "Quote_ID_%s.pdf" % quoteId
w = open(quoteFileName, "wb")
w.write(getPdf.data)
w.close()
查看更多
登录 后发表回答