1
+ # Override IiifPrint::Configuration to allow a config item to limit splitting PDFs by page count (IiifPrint 1.0.0 8fdf56e)
2
+ IiifPrint ::Configuration . class_eval do
3
+ attr_writer :split_pdf_page_limit
4
+ # rubocop:disable Metrics/MethodLength
5
+ # @api private
6
+ # @note These fields will appear in rendering order.
7
+ # @todo To move this to an `@api public` state, we need to consider what a proper configuration looks like.
8
+ def split_pdf_page_limit
9
+ @split_pdf_page_limit ||= 100
10
+ end
11
+ end
12
+
1
13
IiifPrint . config do |config |
2
14
# NOTE: WorkTypes and models are used synonymously here.
3
15
# Add models to be excluded from search so the user
54
66
add_info : { } ,
55
67
collection : { }
56
68
}
69
+
70
+ config . split_pdf_page_limit = 100
71
+
57
72
end
58
73
59
74
# Override Hrax::WorkShowPresenter.authorized_item_ids to disallow "Pdf Page" work type from showing as members
@@ -71,3 +86,29 @@ def authorized_item_ids
71
86
end
72
87
end
73
88
89
+ # Override IiifPrint::SplitPdfs::ChildWorkCreationFromPdfService (IiifPrint 1.0.0 8fdf56e)
90
+ # IiiifPrint rendering does not do well when there are many pages
91
+ # So enforce a page limit over which IiifPRint will not split a PDF
92
+ # into childworks with images for each page
93
+ # Duplicate pagecount from IiifPrint::SplitPdfs::BaseSplitter
94
+ IiifPrint ::SplitPdfs ::ChildWorkCreationFromPdfService . class_eval do
95
+
96
+ PAGE_COUNT_REGEXP = %r{^Pages: +(\d +)$} . freeze
97
+
98
+ def self . pagecount ( pdfpath )
99
+ # Default to a value that will avoid
100
+ # IiifPrint splitting from happening
101
+ pagecount = IiifPrint . config . split_pdf_page_limit +1
102
+ cmd = "pdfinfo #{ pdfpath } "
103
+ Open3 . popen3 ( cmd ) do |_stdin , stdout , _stderr , _wait_thr |
104
+ match = PAGE_COUNT_REGEXP . match ( stdout . read )
105
+ pagecount = match [ 1 ] . to_i
106
+ end
107
+ pagecount
108
+ end
109
+
110
+ def self . pdfs_only_for ( paths )
111
+ paths . select { |path | path . end_with? ( '.pdf' , '.PDF' ) && pagecount ( path ) < IiifPrint . config . split_pdf_page_limit }
112
+ end
113
+ end
114
+
0 commit comments