Datalogics PDF填写Rails

时间:2015-06-24 02:31:17

标签: ruby-on-rails pdf

我正在尝试使用Datalogics PDF(https://api.datalogics-cloud.com/docs#fillform)在我的rails应用程序中填写可填写的pdf(用adobe acrobat制作)。

我很难搞清楚如何拨打api电话。即我无法弄清楚在哪里/如何放置参数。任何帮助深表感谢!谢谢!

其中一个示例是使用curl,因此在控制器操作中,我放置了curl https://pdfprocess.datalogics.com/api/actions/render/pages --insecure --form 'application={"id": "xxxxx", "key": "xxxxxxx"}' --form input=@hello_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 input=@private/fillable/CG1218_6-95.pdf --form filename=@input.json --output flattened.pdf`

1 个答案:

答案 0 :(得分: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)