adjust preview cache

This commit is contained in:
Pete Matsyburka
2026-08-07 21:45:56 +03:00
parent 485b7f2c8f
commit eeec852b3b
3 changed files with 90 additions and 19 deletions
+38
View File
@@ -98,6 +98,7 @@ class Pdfium
attach_function :FPDF_LoadDocument, %i[string FPDF_STRING], :FPDF_DOCUMENT
attach_function :FPDF_LoadMemDocument, %i[pointer int FPDF_STRING], :FPDF_DOCUMENT
attach_function :FPDF_LoadCustomDocument, %i[pointer FPDF_STRING], :FPDF_DOCUMENT
attach_function :FPDF_CloseDocument, [:FPDF_DOCUMENT], :void
attach_function :FPDF_GetPageCount, [:FPDF_DOCUMENT], :int
attach_function :FPDF_GetLastError, [], :ulong
@@ -463,6 +464,43 @@ class Pdfium
end
end
def self.open_io(io, password = nil)
io.binmode
get_block = FFI::Function.new(:int, %i[pointer ulong pointer ulong]) do |_param, position, out, size|
io.seek(position)
bytes = io.read(size).to_s
out.put_bytes(0, bytes)
bytes.bytesize == size ? 1 : 0
end
file_access = Pdfium::FPDF_FILEACCESS.new
file_access[:m_FileLen] = io.size
file_access[:m_GetBlock] = get_block
file_access[:m_Param] = FFI::Pointer::NULL
doc_ptr = Pdfium.FPDF_LoadCustomDocument(file_access, password)
if doc_ptr.null?
Pdfium.check_last_error('Failed to load document from IO')
raise PdfiumError, 'Failed to load document from IO, pointer is NULL.'
end
doc = new(doc_ptr, [file_access, get_block, io])
return doc unless block_given?
begin
yield doc
ensure
doc.close
end
end
def closed?
@closed
end
+2 -2
View File
@@ -192,8 +192,8 @@ module Templates
end
end
def generate_pdf_preview_from_file(attachment, file_path, page_number)
doc = Pdfium::Document.open_file(file_path)
def generate_pdf_preview_from_io(attachment, io, page_number)
doc = Pdfium::Document.open_io(io)
doc_page = doc.get_page(page_number)