2015-06-24 74 views
4

我試圖在我的Rails應用程序中使用Datalogics PDF(https://api.datalogics-cloud.com/docs#fillform)填寫可輸入的pdf(使用Adobe Acrobat製作)。Datalogics PDF填充軌道

我很難搞清楚如何進行api調用。即我無法弄清楚在哪裏/如何放置參數。任何幫助深表感謝!謝謝!

其中一個例子是使用curl,所以在控制器操作中,我把curl https://pdfprocess.datalogics.com/api/actions/render/pages --insecure --form 'application={"id": "xxxxx", "key": "xxxxxxx"}' --form [email protected]_world.pdf --form inputName=hello_world.pdf --output flattened.pdf這使pdf hello_world(位於我的rails根目錄中)變成了一個名爲flattened.pdf的pdf。

雖然我不確定如何理解此代碼。

此外,我想過的不是使用控制器,而是使用表單,其操作是url,並具有各種字段的標籤,這是否是一種有效的可能性?

對於填表,我試圖這樣curl命令:

`curl https://pdfprocess.datalogics.com/api/actions/fill/form --insecure --form 'application={"id": "xxxxx", "key": "xxxxxx"}' --form [email protected]/fillable/CG1218_6-95.pdf --form [email protected] --output flattened.pdf` 
+1

您能向我們展示您到目前爲止所得到的結果以及您收到的任何錯誤嗎?還有什麼你試過等? (注意:不要把它放在評論中,編輯你的問題並在那裏添加它,因爲這些東西應該是你問題的一部分) –

回答

0

您可以使用HTTP請求來做到這一點。我把一些代碼放在一起,你可以在下面看到。您的請求需要成爲多部分,這就是爲什麼您必須定義邊界。我下面的請求是針對DocumentProperties服務的。您需要稍微修改它以添加填寫表單的值。請注意,在傳入文件時,您需要使用「File.read()」讀取文件中的值。

您可以在下面看到相關的代碼。

# Initialize a new HTTPS request object with our URL 
http = Net::HTTP.new(url.host, url.port) 
http.use_ssl = true 
http.verify_mode = OpenSSL::SSL::VERIFY_NONE 

## 
# The Content-Type field for multipart entities requires one parameter, "boundary", 
# which is used to specify the encapsulation boundary. The encapsulation boundary 
# is defined as a line consisting entirely of two hyphen characters ("-", decimal code 45) 
# followed by the boundary parameter value from the Content-Type header field. 

BOUNDARY = 'BoundaryString' 

# Initialize a new Post request 
request = Net::HTTP::Post.new(url) 
request['content-type'] = "multipart/form-data; boundary=#{BOUNDARY}" 


# Initialize a new helper array to aid us set up the post reuqest 
post_body = [] 

# Add the application data, key and ID to the helper array 
post_body << "--#{BOUNDARY}\r\n" 
post_body << "Content-Disposition: form-data; name=\"application\"\r\n\r\n" 
post_body << "{\"id\":\"#{app_id}\", \"key\":\"#{key}\"}" 
post_body << "\r\n--#{BOUNDARY}\r\n" 


# Add the file Data to the helper array 
post_body << "--#{BOUNDARY}\r\n" 
post_body << "Content-Disposition: form-data; name=\"input\"; filename=\"#{File.basename(file)}\"\r\n" 
post_body << "Content-Type: application/pdf\r\n\r\n" 

# Read the file data to the helper array 
# Note that this reads the complete file in memory, and might not work well for large files 
post_body << File.read(file) 
post_body << "\r\n\r\n--#{BOUNDARY}--\r\n" 

# Create the request body by joining all fields of the helper array together 
request.body = post_body.join 

# Submit the post request 
response = http.request(request)