Class: FindingAidPDF
- Inherits:
-
Object
- Object
- FindingAidPDF
- Defined in:
- public/app/models/finding_aid_pdf.rb
Constant Summary collapse
- DEPTH_1_LEVELS =
['collection', 'recordgrp', 'series']
- DEPTH_2_LEVELS =
['subgrp', 'subseries', 'subfonds']
Instance Attribute Summary collapse
-
#archivesspace ⇒ Object
readonly
Returns the value of attribute archivesspace.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#repo_code ⇒ Object
readonly
Returns the value of attribute repo_code.
-
#repo_id ⇒ Object
readonly
Returns the value of attribute repo_id.
-
#resource_id ⇒ Object
readonly
Returns the value of attribute resource_id.
Instance Method Summary collapse
-
#generate ⇒ Object
-
#initialize(repo_id, resource_id, archivesspace_client, base_url) ⇒ FindingAidPDF
constructor
A new instance of FindingAidPDF.
-
#short_title ⇒ Object
-
#source_file ⇒ Object
-
#suggested_filename ⇒ Object
Constructor Details
#initialize(repo_id, resource_id, archivesspace_client, base_url) ⇒ FindingAidPDF
Returns a new instance of FindingAidPDF.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'public/app/models/finding_aid_pdf.rb', line 17 def initialize(repo_id, resource_id, archivesspace_client, base_url) @repo_id = repo_id @resource_id = resource_id @archivesspace = archivesspace_client @base_url = base_url @resource = archivesspace.get_record("/repositories/#{repo_id}/resources/#{resource_id}") @ordered_records = archivesspace.get_record("/repositories/#{repo_id}/resources/#{resource_id}/ordered_records") # make sure finding aid title isn't only like /^\n$/ if @resource.finding_aid['title'] and @resource.finding_aid['title'] =~ /\w/ @short_title = @resource.finding_aid['title'].lstrip.split("\n")[0].strip end end |
Instance Attribute Details
#archivesspace ⇒ Object (readonly)
Returns the value of attribute archivesspace
15 16 17 |
# File 'public/app/models/finding_aid_pdf.rb', line 15 def archivesspace @archivesspace end |
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url
15 16 17 |
# File 'public/app/models/finding_aid_pdf.rb', line 15 def base_url @base_url end |
#repo_code ⇒ Object (readonly)
Returns the value of attribute repo_code
15 16 17 |
# File 'public/app/models/finding_aid_pdf.rb', line 15 def repo_code @repo_code end |
#repo_id ⇒ Object (readonly)
Returns the value of attribute repo_id
15 16 17 |
# File 'public/app/models/finding_aid_pdf.rb', line 15 def repo_id @repo_id end |
#resource_id ⇒ Object (readonly)
Returns the value of attribute resource_id
15 16 17 |
# File 'public/app/models/finding_aid_pdf.rb', line 15 def resource_id @resource_id end |
Instance Method Details
#generate ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'public/app/models/finding_aid_pdf.rb', line 209 def generate java_import com.lowagie.text.pdf.BaseFont; out_html = source_file pdf_file = Tempfile.new pdf_file.close renderer = org.xhtmlrenderer.pdf.ITextRenderer.new resolver = renderer.getFontResolver # ANW-1075: Use Kurinto, followed by Noto Serif by defaults for open source compatibility and Unicode support for Latin, Cyrillic and Greek alphabets # Additional fonts can be specified via config file and added via plugin if AppConfig[:plugins].include?("custom-pui-pdf-font") font_paths = AppConfig[:pui_pdf_font_files].map do |font| Rails.root.to_s + "/../plugins/custom-pui-pdf-font/public/app/assets/fonts/#{font}" end else font_paths = AppConfig[:pui_pdf_font_files].map do |font| Rails.root.to_s + "/app/assets/fonts/#{font}" end end font_paths.each do |font_path| resolver.addFont( font_path, "Identity-H", true ); end renderer.set_document(java.io.File.new(out_html.path)) # FIXME: We'll need to test this with a reverse proxy in front of it. renderer.shared_context.base_url = base_url renderer.layout pdf_output_stream = java.io.FileOutputStream.new(pdf_file.path) renderer.create_pdf(pdf_output_stream) pdf_output_stream.close out_html.unlink pdf_file end |
#short_title ⇒ Object
40 41 42 |
# File 'public/app/models/finding_aid_pdf.rb', line 40 def short_title @short_title || suggested_filename end |
#source_file ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'public/app/models/finding_aid_pdf.rb', line 44 def source_file begin # We'll use the original controller so we can find and render the PDF # partials, but just for its ERB rendering. renderer = PdfController.new start_time = Time.now @repo_code = @resource.repository_information.fetch('top').fetch('repo_code') # .length == 1 would be just the resource itself. has_children = @ordered_records.entries.length > 1 out_html = Tempfile.new # Use a NokogiriPushParser-based writer = Nokogiri::XML::SAX::PushParser.new(XMLCleaner.new(out_html)) begin writer.write(renderer.render_to_string partial: 'header', layout: false, :locals => {:record => @resource}) rescue => e raise PDFRenderErrorHeader, "#{e.class};#{e.message}" end begin writer.write(renderer.render_to_string partial: 'titlepage', layout: false, :locals => {:record => @resource}) rescue => e raise PDFRenderErrorTitlePage, "#{e.class};#{e.message}" end # Drop the resource and filter the AOs toc_aos = @ordered_records.entries.drop(1).select {|entry| if entry.depth == 1 DEPTH_1_LEVELS.include?(entry.level) elsif entry.depth == 2 DEPTH_2_LEVELS.include?(entry.level) else false end } begin writer.write(renderer.render_to_string partial: 'toc', layout: false, :locals => {:resource => @resource, :has_children => has_children, :ordered_aos => toc_aos}) rescue => e raise PDFRenderErrorTOC, "#{e.class};#{e.message}" end begin writer.write(renderer.render_to_string partial: 'resource', layout: false, :locals => {:record => @resource, :has_children => has_children}) rescue => e raise PDFRenderErrorResource, "#{e.class};#{e.message}" end page_size = 50 @ordered_records.entries.drop(1).each_slice(page_size) do |entry_set| if AppConfig[:pui_pdf_timeout] && AppConfig[:pui_pdf_timeout] > 0 && (Time.now.to_i - start_time.to_i) >= AppConfig[:pui_pdf_timeout] raise TimeoutError.new("PDF generation timed out. Sorry!") end uri_set = entry_set.map(&:uri) record_set = archivesspace.search_records(uri_set, {}, true).records unprocessed_record_list = record_set.zip(entry_set) ao_list = [] # tuple looks like [ArchivalObject, Entry] unprocessed_record_list.each_with_index do |tuple, i| record = tuple[0] next_record = unprocessed_record_list[i + 1][0] rescue nil next unless record.is_a?(ArchivalObject) if next_record && record.uri == next_record.parent_for_md_mapping has_children = true else has_children = false end tuple[2] = has_children ao_list.push(tuple) end ao_list.each do |record, entry, is_parent| begin writer.write(renderer.render_to_string partial: 'archival_object', layout: false, :locals => {:record => record, :level => entry.depth, :is_parent => is_parent}) rescue => e = e. + " (while processing Archival Object '#{record['title']}')" raise PDFRenderErrorArchivalObject, "#{e.class};#{message}" end end end begin writer.write(renderer.render_to_string partial: 'footer', layout: false, :locals => {:record => @resource}) rescue => e raise , "#{e.class};#{e.message}" end out_html.close out_html rescue => e out_html = Tempfile.new writer = Nokogiri::XML::SAX::PushParser.new(XMLCleaner.new(out_html)) location = case e.class.to_s when "PDFRenderErrorHeader" I18n.t('pdf_error.location.location_header') when "PDFRenderErrorTitlePage" I18n.t('pdf_error.location.location_title_page') when "PDFRenderErrorTOC" I18n.t('pdf_error.location.location_toc') when "PDFRenderErrorResource" I18n.t('pdf_error.location.location_resource') when "PDFRenderErrorArchivalObject" I18n.t('pdf_error.location.location_archival_object') when "PDFRenderErrorFooter" I18n.t('pdf_error.location.location_footer') else nil end # nil means some other error occured if location == nil = e. orig_class = e.class.to_s else orig_class, = e..split(';') end error_html = "" error_html += "<body>" error_html += "<h1>#{I18n.t('pdf_error.title')}</h1>" error_html += "<p>#{I18n.t('pdf_error.description')}</p>" unless location == nil error_html += "<p><b>#{I18n.t('pdf_error.headings.location')}</b></p>" error_html += "<p>#{location}</p>" end error_html += "<p><b>#{I18n.t('pdf_error.headings.message')}</b></p>" error_html += "<p>#{message}</p>" error_html += "<p><b>#{I18n.t('pdf_error.headings.type')}</b></p>" error_html += "<p>#{orig_class}</p>" if orig_class == "Nokogiri::XML::SyntaxError" error_html += "<p><b>#{I18n.t('pdf_error.headings.additional_info')}</b></p>" error_html += "<p>#{I18n.t('pdf_error.additional_info.invalid_markup')}</p>" end error_html += "</body>" writer.write(error_html) out_html.close out_html end end |
#suggested_filename ⇒ Object
32 33 34 35 36 37 38 |
# File 'public/app/models/finding_aid_pdf.rb', line 32 def suggested_filename # Use the EAD ID. If that's missing, use the 4-part identifier filename = (@resource.ead_id || @resource.four_part_identifier.reject(&:blank?).join('_')) # no spaces, please. filename.gsub(' ', '_') + '.pdf' end |