我正在开发一个使用prawn生成PDF的Rails应用程序。简而言之,我们希望能够对生成的PDF进行数字签名。我不知道从哪里开始准确阅读。只是想问一下其他人之前是否能够完成这项工作,如果是的话,我应该用什么样的资源来做这件事。
谢谢!
答案 0 :(得分:3)
我建议你看看https://github.com/joseicosta/odf-report。 它是生成ODF的宝石,但它们可以很容易地转换为PDF,而且它支持模板。
答案 1 :(得分:2)
虽然这个问题已经回答并且相当陈旧,但我想添加相关链接以供参考。
shallow copy已将代码分享给MrWater。
VERSION 1 - 生成证书和密钥文件,并将它们直接插入到文档中
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)
版本2 - 使用现有证书签署pdf文件
import pygame
from colors import *
class Block (pygame.sprite.Sprite):
def set_message(text):
global message,previous_message
message = font.render(text,True,black,white)
previous_message=message
def __init__(self, color =blue, width=64, height = 64):
super( Block, self ).__init__()
self.image = pygame.Surface((width, height))
self.image.fill( color )
self.rect=self.image.get_rect()
self.sound=pygame.mixer.Sound("")
self.origin_x = self.rect.centerx
self.origin_y = self.rect.centery
def set_properties(self):
self.rect=self.image.get_rect()
self.origin_x = self.rect.centerx
self.origin_y = self.rect.centery
def set_position (self, x, y):
self.rect.x = x-self.origin_x
self.rect.y = y-self.origin_y
def set_image( self, filename = None ):
if (filename !=None ):
self.image = pygame.image.load (filename)
self.set_properties()
def play_sound(self):
self.sound.play(-1)
if ( __name__ == "__main__"):
pygame.init()
window_size = window_width, window_height = 512, 384
window = pygame.display.set_mode(window_size, pygame.RESIZABLE)
pygame.display.set_caption( " Cloud Runner ")
window.fill ( white )
clock = pygame.time.Clock()
frames_per_second = 60
block_group = pygame.sprite.Group()
a_block = Block()
a_block.play_sound()
a_block.set_image("beaten_brick_tiled.png")
a_block.set_position( window_width/2, window_height/2)
another_block = Block( red )
another_block.set_position(100,100)
block_group.add(a_block, another_block)
font = pygame.font.SysFont("Times New Roman, Arial",30)
message = previous_message = None
set_message("Hello World!")
running = True
while ( running ):
pygame.event.pump()
for event in pygame.event.get():
if (event.type == pygame.QUIT) or \
(event.type ==pygame.KEYDOWN and \
(event.key == pygame.K_ESCAPE or event.key ==pygame.K_q)):
running = False
if (event.type == pygame.MOUSEMOTION):
mouse_pos = pygame.mouse.get_pos()
a_block.set_position(mouse_pos[0],mouse_pos[1])
clock.tick (frames_per_second)
window.fill ( white )
if (message != previous_message):
set_message(message)
window.blit(message,(window_width/2 - message.get_rect().width/2,window_height/2-100))
block_group.draw( window )
pygame.display.update()
pygame.quit()