lib/jasper-command-line/jasper.rb in jasper-command-line-0.2.2 vs lib/jasper-command-line/jasper.rb in jasper-command-line-0.2.3
- old
+ new
@@ -79,25 +79,47 @@
# This is here to avoid the "already initialized constant DOCUMENT_POSITION_*" warnings.
JRXmlUtils._invoke('parse', 'Lorg.xml.sax.InputSource;', input_source)
end
jasper_params.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, data_document)
- jasper_print = JasperFillManager.fillReport(jasper_file, jasper_params)
- # Export it!
+ temp_file = Tempfile.new(['pdf-', '.pdf'])
+ file = temp_file.path
+ temp_file.close!
+ created_files = [file]
+
+ pdf = PDF::Merger.new
+
+ # Export n copies and merge them into one file
+ options[:copies] ||= 1
+
+ (1..options[:copies]).each do |copy|
+ copy_temp_file = Tempfile.new(["pdf-#{copy}-", '.pdf'])
+ copy_file = copy_temp_file.path
+ copy_temp_file.close!
+
+ jasper_params.put JavaString.new('copy_number'), JavaString.new(copy.to_s)
+ jasper_print = JasperFillManager.fillReport(jasper_file, jasper_params)
+
+ File.open(copy_file, 'wb') { |f| f.write JasperExportManager._invoke('exportReportToPdf', 'Lnet.sf.jasperreports.engine.JasperPrint;', jasper_print) }
+
+ pdf.add_file copy_file
+
+ created_files << copy_file
+ end
+
+ pdf.save_as file
+
+ # Digitally sign the file, if necessary
if sign_options
- temp_file = Tempfile.new(['pdf-', '.pdf'])
temp_signed_file = Tempfile.new(['signed-pdf-', '.pdf'])
- file = temp_file.path
signed_file = temp_signed_file.path
temp_file.close!
temp_signed_file.close!
- File.open(file, 'wb') { |f| f.write JasperExportManager._invoke('exportReportToPdf', 'Lnet.sf.jasperreports.engine.JasperPrint;', jasper_print) }
-
call_options = [
'-n',
'-t', file,
'-s', sign_options[:key_file],
'-p', %Q["#{sign_options[:password]}"],
@@ -106,19 +128,23 @@
call_options.push '-l', %Q["#{sign_options[:location]}"] if sign_options[:location]
call_options.push '-r', %Q["#{sign_options[:reason]}"] if sign_options[:reason]
`java -jar #{File.dirname(__FILE__)}/java/PortableSigner/PortableSigner.jar #{call_options.join(' ')}`
+
+ created_files << signed_file
+ else
+ signed_file = file
+ end
+
+ begin
+ return File.read(signed_file)
+ ensure
begin
- return File.read(signed_file)
- ensure
- begin
- File.unlink file, signed_file
- rescue
- end
+ File.unlink *created_files
+ rescue => e
+ puts e.message
end
- else
- JasperExportManager._invoke('exportReportToPdf', 'Lnet.sf.jasperreports.engine.JasperPrint;', jasper_print)
end
rescue Exception=>e
if e.respond_to? 'printStackTrace'
JasperCommandLine.logger.error e.message
\ No newline at end of file