Class: ASFop

Inherits:
Object
  • Object
show all
Defined in:
backend/app/lib/AS_fop.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, output = nil, pdf_image) ⇒ ASFop

Returns a new instance of ASFop.



23
24
25
26
27
28
29
30
31
32
33
# File 'backend/app/lib/AS_fop.rb', line 23

def initialize(source, output= nil, pdf_image)
  @source = source
  @output = output ? output : ASUtils.tempfile('fop.pdf')
  if pdf_image.nil?
    @pdf_image = "file:///" + File.absolute_path(StaticAssetFinder.new(File.join('stylesheets')).find('ArchivesSpaceLogo_for_pdf.png'))
  else
    @pdf_image = pdf_image
  end
  @xslt = File.read( StaticAssetFinder.new(File.join('stylesheets')).find('as-ead-pdf.xsl'))
  @config = java.io.File.new(StaticAssetFinder.new(File.join('stylesheets')).find('fop-config.xml'))
end

Instance Attribute Details

#outputObject

Returns the value of attribute output



20
21
22
# File 'backend/app/lib/AS_fop.rb', line 20

def output
  @output
end

#sourceObject

Returns the value of attribute source



19
20
21
# File 'backend/app/lib/AS_fop.rb', line 19

def source
  @source
end

#xsltObject

Returns the value of attribute xslt



21
22
23
# File 'backend/app/lib/AS_fop.rb', line 21

def xslt
  @xslt
end

Instance Method Details

#fop_processorObject



50
51
52
53
# File 'backend/app/lib/AS_fop.rb', line 50

def fop_processor
  fopfac = FopFactory.newInstance(@config)
  fopfac.newFop(MimeConstants::MIME_PDF, @output.to_outputstream)
end

#saxon_processorObject



35
36
37
# File 'backend/app/lib/AS_fop.rb', line 35

def saxon_processor
  @saxon_processor ||= Saxon::Processor.create
end

#to_fo(sax_handler) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'backend/app/lib/AS_fop.rb', line 39

def to_fo(sax_handler)
  transformer = saxon_processor.xslt_compiler.compile(Saxon::Source.create(File.join(ASUtils.find_base_directory, 'stylesheets', 'as-ead-pdf.xsl')))
  sax_destination = Saxon::S9API::SAXDestination.new(sax_handler)
  input = saxon_processor.document_builder.build(Saxon::Source.create(@source))
  params = {"pdf_image" => @pdf_image}
  transformer.apply_templates(input, {
    global_parameters: params,
    global_context_item: input
  }).to_destination(sax_destination)
end

#to_pdfObject

returns a temp file with the converted PDF



56
57
58
59
60
61
62
63
# File 'backend/app/lib/AS_fop.rb', line 56

def to_pdf
  begin
    to_fo(fop_processor.getDefaultHandler)
  ensure
    @output.close
  end
  @output
end

#to_pdf_streamObject



65
66
67
68
69
70
71
72
73
74
# File 'backend/app/lib/AS_fop.rb', line 65

def to_pdf_stream
  begin
    to_fo(fop_processor.getDefaultHandler)
    @output.rewind
    @output.read
  ensure
    @output.close
    @output.unlink
  end
end