Class: Sphragis::PdfSigner
- Inherits:
-
Object
- Object
- Sphragis::PdfSigner
- Defined in:
- lib/sphragis/pdf_signer.rb
Defined Under Namespace
Classes: SigningError
Instance Attribute Summary collapse
-
#pdf_path ⇒ Object
readonly
Returns the value of attribute pdf_path.
-
#provider ⇒ Object
readonly
Returns the value of attribute provider.
-
#signature_options ⇒ Object
readonly
Returns the value of attribute signature_options.
Instance Method Summary collapse
-
#initialize(pdf_path, signature_options = {}) ⇒ PdfSigner
constructor
Initialize with PDF path and optional signature placement.
-
#pdf_info ⇒ Object
Get PDF metadata.
-
#sign ⇒ String
Sign the PDF document.
-
#validate_placement(page_number = nil) ⇒ Object
Validate signature placement on a specific page.
Constructor Details
#initialize(pdf_path, signature_options = {}) ⇒ PdfSigner
Initialize with PDF path and optional signature placement
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/sphragis/pdf_signer.rb', line 24 def initialize(pdf_path, = {}) @pdf_path = pdf_path = .merge() # Create provider based on options or use default provider_name = .delete(:provider) || ProviderFactory.default_provider @provider = ProviderFactory.create(provider_name) validate_pdf! end |
Instance Attribute Details
#pdf_path ⇒ Object (readonly)
Returns the value of attribute pdf_path.
11 12 13 |
# File 'lib/sphragis/pdf_signer.rb', line 11 def pdf_path @pdf_path end |
#provider ⇒ Object (readonly)
Returns the value of attribute provider.
11 12 13 |
# File 'lib/sphragis/pdf_signer.rb', line 11 def provider @provider end |
#signature_options ⇒ Object (readonly)
Returns the value of attribute signature_options.
11 12 13 |
# File 'lib/sphragis/pdf_signer.rb', line 11 def end |
Instance Method Details
#pdf_info ⇒ Object
Get PDF metadata
63 64 65 66 67 68 69 70 71 |
# File 'lib/sphragis/pdf_signer.rb', line 63 def pdf_info reader = PDF::Reader.new(pdf_path) { page_count: reader.page_count, pdf_version: reader.pdf_version, info: reader.info, metadata: reader. } end |
#sign ⇒ String
Sign the PDF document
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/sphragis/pdf_signer.rb', line 37 def sign raise SigningError, "PDF file does not exist: #{pdf_path}" unless File.exist?(pdf_path) @provider.connect # Read the PDF content pdf_content = File.binread(pdf_path) # Create signature data signature_data = create_signature_data(pdf_content) # Sign with provider signature = @provider.sign(signature_data) # Create signed PDF signed_pdf_path = create_signed_pdf(signature) @provider.disconnect signed_pdf_path rescue StandardError => e @provider&.disconnect raise SigningError, "Failed to sign PDF: #{e.message}" end |
#validate_placement(page_number = nil) ⇒ Object
Validate signature placement on a specific page
74 75 76 77 78 79 80 81 |
# File 'lib/sphragis/pdf_signer.rb', line 74 def validate_placement(page_number = nil) page = page_number || [:page] || pdf_info[:page_count] info = pdf_info raise SigningError, "Invalid page number: #{page}" if page > info[:page_count] || page < 1 true end |