-
Notifications
You must be signed in to change notification settings - Fork 2
/
XL_transformer.rb
102 lines (82 loc) · 2.85 KB
/
XL_transformer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
require 'java'
require 'saxon-rb'
require 'stringio'
java_import Java::org::apache::fop::apps::FopFactory
java_import Java::org::apache::fop::apps::Fop
java_import Java::org::apache::fop::apps::MimeConstants
class XLTransformer
import javax.xml.transform.stream.StreamSource
import javax.xml.transform.TransformerFactory
import javax.xml.transform.sax.SAXResult
attr_accessor :source
attr_accessor :output
attr_accessor :xslt
attr_accessor :xslt_path
attr_accessor :config
attr_accessor :base
def initialize(source, xslt_path, base, output= nil, config = nil)
@source = source
@output = output ? output : ASUtils.tempfile('xform.pdf')
@config = config
@base = base
if [email protected]_with?('http')
@base = "file:///#{@base}"
end
if [email protected]_with?('/')
@base << '/'
end
@xslt_path = xslt_path
@xslt = File.read(@xslt_path)
end
def transform(params = {})
transformer = Saxon.XSLT(@xslt, system_id: @xslt_path )
transformer.transform(Saxon.XML(@source), params).to_s
end
# def to_fo
# transformer = Saxon.XSLT(@xslt, system_id: File.join(ASUtils.find_base_directory, 'stylesheets', 'as-ead-pdf.xsl') )
# transformer.transform(Saxon.XML(@source), {"pdf_image" => "\'#{@pdf_image}\'"}).to_s
# end
# returns a temp file with the converted PDF
def to_pdf
begin
params = {
"primary-font-for-pdf" => "'Open_Sans'",
"sans-serif-font" => "'Open_Sans'",
"serif-font" => "'Open_Sans'",
"backup-font" =>"'NotoSans, KurintoText'",
# "logo-location" => "'https://raw.githubusercontent.com/YaleArchivesSpace/EAD3-to-PDF-UA/master/'",
"logo-location" => "'#{@base}'",
"suppressInternalComponentsInPDF" => "false()"
}
fo = StringIO.new(transform(params)).to_inputstream
fopfac = FopFactory.newInstance
# Log.error("Settin fop base to #{@base}")
fopfac.setBaseURL( @base )
fopfac.setUserConfig(@config)
fop = fopfac.newFop(MimeConstants::MIME_PDF, @output.to_outputstream)
pdftransformer = TransformerFactory.newInstance.newTransformer()
res = SAXResult.new(fop.getDefaultHandler)
pdftransformer.transform(StreamSource.new(fo), res)
ensure
@output.close
end
@output
end
# def to_pdf_stream
# begin
# fo = StringIO.new(to_fo).to_inputstream
# fopfac = FopFactory.newInstance
# fopfac.setBaseURL( File.join(ASUtils.find_base_directory, 'stylesheets') )
# fopfac.setUserConfig(@config)
# fop = fopfac.newFop(MimeConstants::MIME_PDF, @output.to_outputstream)
# transformer = TransformerFactory.newInstance.newTransformer()
# res = SAXResult.new(fop.getDefaultHandler)
# transformer.transform(StreamSource.new(fo), res)
# @output.rewind
# @output.read
# ensure
# @output.close
# @output.unlink
# end
# end
end