From 887d80f6d7079fc1a0c35028fadc42d613a7ed9b Mon Sep 17 00:00:00 2001 From: Pete Matsyburka Date: Sat, 5 Sep 2026 13:09:09 +0300 Subject: [PATCH] raw page size --- lib/pdfium.rb | 28 ++++++++++++++++++++++++++++ lib/templates/modify_documents.rb | 5 +---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/pdfium.rb b/lib/pdfium.rb index 470c2ccf..f5d46396 100644 --- a/lib/pdfium.rb +++ b/lib/pdfium.rb @@ -105,6 +105,7 @@ class Pdfium 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_GetPageSizeByIndexF, %i[FPDF_DOCUMENT int pointer], :int attach_function :FPDF_GetLastError, [], :ulong attach_function :FPDF_GetTrailerEnds, %i[FPDF_DOCUMENT pointer ulong], :ulong attach_function :FPDF_DocumentHasValidCrossReferenceTable, [:FPDF_DOCUMENT], :int @@ -282,6 +283,7 @@ class Pdfium attach_function :FPDFPage_GetAnnotCount, [:FPDF_PAGE], :int attach_function :FPDFPage_GetAnnotCountRaw, %i[FPDF_DOCUMENT int], :int + attach_function :FPDFPage_GetRotationRaw, %i[FPDF_DOCUMENT int], :int attach_function :FPDFPage_GetAnnot, %i[FPDF_PAGE int], :FPDF_ANNOTATION attach_function :FPDFPage_CloseAnnot, [:FPDF_ANNOTATION], :void attach_function :FPDFAnnot_GetSubtype, [:FPDF_ANNOTATION], :int @@ -368,6 +370,11 @@ class Pdfium :bottom, :float end + class FS_SIZEF < FFI::Struct + layout :width, :float, + :height, :float + end + class FS_MATRIX < FFI::Struct layout :a, :float, :b, :float, @@ -531,6 +538,8 @@ class Pdfium @pages = {} @annot_counts = {} + @page_sizes = {} + @page_rotations = {} @closed = false @source_buffer = source_buffer @form_handle = FFI::Pointer::NULL @@ -555,6 +564,23 @@ class Pdfium @page_count ||= Pdfium.FPDF_GetPageCount(@document_ptr) end + def page_size(page_index) + @page_sizes[page_index] ||= + begin + size = Pdfium::FS_SIZEF.new + + [size[:width], size[:height]] if Pdfium.FPDF_GetPageSizeByIndexF(@document_ptr, page_index, size) == 1 + end + end + + def reset_page_size(page_index) + @page_sizes.delete(page_index) + end + + def page_rotation(page_index) + @page_rotations[page_index] ||= Pdfium.FPDFPage_GetRotationRaw(@document_ptr, page_index) + end + def encrypted? Pdfium.FPDF_GetSecurityHandlerRevision(@document_ptr) >= 0 end @@ -983,6 +1009,8 @@ class Pdfium def rotation=(value) Pdfium.FPDFPage_SetRotation(@page_ptr, value) + @document.reset_page_size(@page_index) + @rotation = value end diff --git a/lib/templates/modify_documents.rb b/lib/templates/modify_documents.rb index 5be3b07b..d10cd97d 100644 --- a/lib/templates/modify_documents.rb +++ b/lib/templates/modify_documents.rb @@ -345,10 +345,7 @@ module Templates uuid = pdf_ref['attachment_uuid'] source = sources[[uuid, nil]] ||= open_or_build_pdf(attachments_index[uuid]) - page = source.get_page(pdf_ref['page']) - - width = page.width - height = page.height + width, height = source.page_size(pdf_ref['page']) width, height = height, width unless (pdf_ref['rotate'].to_i % 180).zero?