我已经实现了一个本地脚本,将数字签名插入到Origami中反复出现的本地pdf文件中,但是不太知道在rails服务器和amazon s3存储文件中执行此操作的最佳方法是什么。
我猜我需要将文件从s3下载到我的服务器(或者在上传到亚马逊之前捕获它,这是我用回形针做的)插入签名,然后再将它发送回s3。
以下是折纸解决方案的pdf.rb文件中的PDF.read方法:
class << self
#
# Reads and parses a PDF file from disk.
#
def read(filename, options = {})
filename = File.expand_path(filename) if filename.is_a?(::String)
PDF::LinearParser.new(options).parse(filename)
end
我如何调整这个以便我处理内存中的二进制文件?
你有什么建议吗?
您可以找到有关折纸here
的更多信息我的代码
require 'openssl'
begin
require 'origami'
rescue LoadError
ORIGAMIDIR = "C:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\origami-1.2.4\lib"
$: << ORIGAMIDIR
require 'origami'
end
include Origami
INPUTFILE = "Sample.pdf"
@inputfile = String.new(INPUTFILE)
OUTPUTFILE = @inputfile.insert(INPUTFILE.rindex("."),"_signed")
CERTFILE = "certificate.pem"
RSAKEYFILE = "private_key.pem"
passphrase = "your passphrase"
key4pem=File.read RSAKEYFILE
key = OpenSSL::PKey::RSA.new key4pem, passphrase
cert = OpenSSL::X509::Certificate.new(File.read CERTFILE)
pdf = PDF.read(INPUTFILE)
page = pdf.get_page(1)
# Add signature annotation (so it becomes visibles in pdf document)
sigannot = Annotation::Widget::Signature.new
sigannot.Rect = Rectangle[:llx => 89.0, :lly => 386.0, :urx => 190.0, :ury => 353.0]
page.add_annot(sigannot)
# Sign the PDF with the specified keys
pdf.sign(cert, key,
:method => 'adbe.pkcs7.sha1',
:annotation => sigannot,
:location => "Portugal",
:contact => "myemail@email.tt",
:reason => "Proof of Concept"
)
# Save the resulting file
pdf.save(OUTPUTFILE)
答案 0 :(得分:0)
在对折纸code进行深入分析后,我注意到PDF.Read接受二进制文件,因此我们可以将文件实例作为一个整体发送,而不是发送本地文件路径。 p>
答案 1 :(得分:0)
PDF.read
和PDF.save
方法都接受文件路径或Ruby IO对象。
从字符串创建PDF实例的一种方法(我想,当你说“内存中”时,你的意思是使用StringIO对象。
例如,Origami shell中的以下会话将创建一个PDF实例,将其保存到StringIO对象并使用自己的输出字符串重新加载它。
>>> PDF.new.save(strio = StringIO.new)
...
>>> strio.string
"%PDF-1.0\r\n1 0 obj\r\n<<\r\n\t/Pages 2 0 R ..."
>>> strio.reopen(strio.string, 'r')
#<StringIO:0xffbea6cc>
>>> pdf = PDF.read(strio)
...
>>> pdf.class
Origami::PDF
答案 2 :(得分:0)
正如@MrWater 所写,Origami::PDF.read
接受流(更准确地说,Origami::PDF::LinearParser
接受,查看源 here)。
这是我的简单解决方案:
require 'open-uri'
# pdf_url = 'http://someurl.com/somepdf.pdf'
pdf = Origami::PDF.read(URI.parse(pdf_url))