2021-04-06 19:56:24 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Reports
|
|
|
|
class PdfJob < ApplicationJob
|
2021-05-11 19:28:28 +08:00
|
|
|
extend InputSanitizeHelper
|
2021-04-06 19:56:24 +08:00
|
|
|
include InputSanitizeHelper
|
2021-04-12 00:19:40 +08:00
|
|
|
include ReportsHelper
|
2021-11-24 18:01:57 +08:00
|
|
|
include Canaid::Helpers::PermissionsHelper
|
2023-09-06 18:51:34 +08:00
|
|
|
include FailedDeliveryNotifiableJob
|
2021-04-06 19:56:24 +08:00
|
|
|
|
2021-06-08 20:01:37 +08:00
|
|
|
PDFUNITE_ENCRYPTED_PDF_ERROR_STRING = 'Unimplemented Feature: Could not merge encrypted files'
|
|
|
|
|
2021-04-06 19:56:24 +08:00
|
|
|
queue_as :reports
|
|
|
|
|
2021-04-23 04:28:50 +08:00
|
|
|
PREVIEW_EXTENSIONS = %w(docx pdf).freeze
|
|
|
|
|
2023-09-06 18:51:34 +08:00
|
|
|
def perform(report_id, user_id:)
|
2023-11-03 20:19:46 +08:00
|
|
|
@report = Report.find(report_id)
|
|
|
|
@user = User.find(user_id)
|
|
|
|
@file = Tempfile.new(['report', '.pdf'], binmode: true)
|
|
|
|
initialize_template
|
|
|
|
set_renderer_context
|
|
|
|
generate_pdf_content
|
|
|
|
process_attach_pdf_report_and_notify
|
2023-09-06 18:51:34 +08:00
|
|
|
rescue StandardError => e
|
2023-11-03 20:19:46 +08:00
|
|
|
raise e if @report.blank?
|
2023-09-06 18:51:34 +08:00
|
|
|
|
|
|
|
ActiveRecord::Base.no_touching do
|
2023-11-03 20:19:46 +08:00
|
|
|
@report.pdf_error!
|
2023-09-06 18:51:34 +08:00
|
|
|
end
|
2023-11-03 20:19:46 +08:00
|
|
|
Rails.logger.error("Couldn't generate PDF for Report with id: #{@report.id}. Error:\n #{e.message}")
|
2023-09-06 18:51:34 +08:00
|
|
|
raise e
|
2023-11-03 20:19:46 +08:00
|
|
|
ensure
|
|
|
|
Rails.application.config.x.custom_sanitizer_config = nil
|
|
|
|
I18n.backend.date_format = nil
|
|
|
|
@file.close(true)
|
2021-04-06 19:56:24 +08:00
|
|
|
end
|
2021-04-23 04:28:50 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-11-03 20:19:46 +08:00
|
|
|
def initialize_template
|
|
|
|
@template = if Extends::REPORT_TEMPLATES.key?(@report.settings[:template]&.to_sym)
|
|
|
|
@report.settings[:template]
|
|
|
|
else
|
|
|
|
Extends::REPORT_TEMPLATES.keys.first.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
raise StandardError, 'Report template not found!' if @template.blank?
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_renderer_context
|
|
|
|
I18n.backend.date_format = @user.settings[:date_format]
|
|
|
|
ActionController::Renderer::RACK_KEY_TRANSLATION['warden'] ||= 'warden'
|
|
|
|
proxy = Warden::Proxy.new({}, Warden::Manager.new({}))
|
|
|
|
proxy.set_user(@user, scope: :user, store: false)
|
|
|
|
ApplicationController.renderer.defaults[:http_host] = Rails.application.routes.default_url_options[:host]
|
|
|
|
@renderer = ApplicationController.renderer.new(warden: proxy)
|
|
|
|
Rails.application.config.x.custom_sanitizer_config = build_custom_sanitizer_config
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_pdf_content
|
2024-04-09 15:25:47 +08:00
|
|
|
@num_of_cover_pages = cover_pages_count
|
2023-11-03 20:19:46 +08:00
|
|
|
|
|
|
|
render_header_footer_and_report
|
|
|
|
|
|
|
|
gather_styles_and_scripts
|
|
|
|
|
|
|
|
generate_pdf_file
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_header_footer_and_report
|
|
|
|
@header_html = @renderer.render_to_string(
|
|
|
|
template: "reports/templates/#{@template}/header",
|
|
|
|
layout: false,
|
|
|
|
locals: { report: @report, user: @user, logo: report_logo }
|
|
|
|
)
|
|
|
|
@footer_html = @renderer.render_to_string(
|
|
|
|
template: "reports/templates/#{@template}/footer",
|
|
|
|
layout: false,
|
|
|
|
locals: { report: @report, user: @user, logo: report_logo }
|
|
|
|
)
|
|
|
|
@report_html = @renderer.render_to_string(
|
|
|
|
template: 'reports/report',
|
|
|
|
layout: false,
|
|
|
|
assigns: { settings: @report.settings },
|
2024-04-09 15:25:47 +08:00
|
|
|
locals: { report: @report, user: @user, num_of_cover_pages: @num_of_cover_pages }
|
2023-11-03 20:19:46 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def gather_styles_and_scripts
|
|
|
|
css_files = [
|
|
|
|
'application',
|
|
|
|
'reports_pdf',
|
|
|
|
'bootstrap_pack',
|
|
|
|
'handsontable.formula'
|
|
|
|
]
|
|
|
|
|
|
|
|
javascript_files = [
|
|
|
|
'jquery_bundle',
|
|
|
|
'handsontable.full',
|
|
|
|
'lodash',
|
|
|
|
'numeral',
|
|
|
|
'numeric',
|
|
|
|
'md5',
|
|
|
|
'jstat',
|
|
|
|
'formula',
|
|
|
|
'parser',
|
|
|
|
'ruleJS',
|
|
|
|
'big.min',
|
|
|
|
'handsontable.formula',
|
|
|
|
'reports/content',
|
|
|
|
'reports/template_helpers'
|
|
|
|
]
|
|
|
|
|
|
|
|
@style_tag_options = []
|
|
|
|
@script_tag_options = []
|
|
|
|
|
|
|
|
@style_tag_options = css_files.map do |file_name|
|
|
|
|
{ content: fetch_asset_content("#{file_name}.css") }
|
|
|
|
end
|
|
|
|
|
|
|
|
@style_tag_options.concat(font_awesome_links)
|
|
|
|
|
|
|
|
@script_tag_options = javascript_files.map do |file_name|
|
|
|
|
{ content: fetch_asset_content("#{file_name}.js") }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_pdf_file
|
|
|
|
current_margin = extract_margins_from_header ||
|
|
|
|
{ top: '2cm', bottom: '2cm', left: '1cm', right: '1.5cm' }
|
|
|
|
|
|
|
|
Grover.new(
|
|
|
|
@report_html,
|
|
|
|
format: 'A4',
|
|
|
|
print_background: true,
|
|
|
|
margin: current_margin,
|
|
|
|
display_header_footer: true,
|
|
|
|
header_template: @header_html,
|
|
|
|
footer_template: @footer_html,
|
|
|
|
style_tag_options: @style_tag_options,
|
|
|
|
script_tag_options: @script_tag_options,
|
2024-04-09 15:25:47 +08:00
|
|
|
page_ranges: "#{@num_of_cover_pages + 1}-999999",
|
2023-11-03 20:19:46 +08:00
|
|
|
emulate_media: 'screen',
|
2023-11-14 16:48:29 +08:00
|
|
|
display_url: Rails.application.routes.default_url_options[:host]
|
2023-11-03 20:19:46 +08:00
|
|
|
).to_pdf(@file.path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_attach_pdf_report_and_notify
|
|
|
|
@file.rewind
|
2024-04-09 15:25:47 +08:00
|
|
|
@file = prepend_title_page if @num_of_cover_pages.positive?
|
2023-11-03 20:19:46 +08:00
|
|
|
@file = append_result_asset_previews if @report.settings.dig(:task, :file_results_previews)
|
|
|
|
|
|
|
|
@report.pdf_file.attach(io: @file, filename: 'report.pdf')
|
|
|
|
@report.pdf_ready!
|
|
|
|
|
|
|
|
create_notification_for_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_notification_for_user
|
|
|
|
report_path = Rails.application.routes.url_helpers
|
|
|
|
.reports_path(team: @report.team.id, preview_report_id: @report.id, preview_type: :pdf)
|
2023-12-08 20:57:43 +08:00
|
|
|
DeliveryNotification.send_notifications(
|
|
|
|
{
|
|
|
|
title: I18n.t('projects.reports.index.generation.completed_pdf_notification_title'),
|
|
|
|
message: I18n.t('projects.reports.index.generation.completed_notification_message',
|
|
|
|
report_link: "<a href='#{report_path}'>#{escape_input(@report.name)}</a>",
|
|
|
|
team_name: escape_input(@report.team.name)),
|
2023-12-12 19:11:52 +08:00
|
|
|
subject_id: @report.id,
|
|
|
|
subject_class: 'Report',
|
|
|
|
subject_name: @report.name,
|
|
|
|
report_type: 'pdf',
|
2023-12-08 20:57:43 +08:00
|
|
|
user: @user
|
|
|
|
}
|
|
|
|
)
|
2023-11-03 20:19:46 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def append_result_asset_previews
|
2021-04-23 04:28:50 +08:00
|
|
|
Dir.mktmpdir do |tmp_dir|
|
2023-11-03 20:19:46 +08:00
|
|
|
@report.report_elements.my_module.each do |my_module_element|
|
|
|
|
next unless can_read_my_module?(@user, my_module_element.my_module)
|
2021-11-24 18:01:57 +08:00
|
|
|
|
2021-05-20 21:54:36 +08:00
|
|
|
results = my_module_element.my_module.results
|
2023-11-03 20:19:46 +08:00
|
|
|
order_results_for_report(results, @report.settings.dig(:task, :result_order)).each do |result|
|
2023-07-26 21:29:13 +08:00
|
|
|
result.assets.each do |asset|
|
|
|
|
next unless PREVIEW_EXTENSIONS.include?(asset.file.blob.filename.extension)
|
|
|
|
|
|
|
|
if !asset.file_pdf_preview.attached? || (asset.file.created_at > asset.file_pdf_preview.created_at)
|
|
|
|
PdfPreviewJob.perform_now(asset.id)
|
|
|
|
asset.reload
|
|
|
|
end
|
|
|
|
asset.file_pdf_preview.open(tmpdir: tmp_dir) do |file|
|
2023-11-03 20:19:46 +08:00
|
|
|
@file = merge_pdf_files(file, @file)
|
2023-07-26 21:29:13 +08:00
|
|
|
end
|
2021-04-23 04:28:50 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-11-03 20:19:46 +08:00
|
|
|
@file
|
2021-04-23 04:28:50 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def merge_pdf_files(file, report_file)
|
|
|
|
merged_file = Tempfile.new(['report', '.pdf'], binmode: true)
|
2021-06-08 20:01:37 +08:00
|
|
|
|
|
|
|
_output, error, status = Open3.capture3(
|
2021-04-23 04:28:50 +08:00
|
|
|
'pdfunite', report_file.path, file.path, merged_file.path
|
|
|
|
)
|
|
|
|
|
2021-06-08 20:01:37 +08:00
|
|
|
# don't raise error if the issue was an encrypted pdf, which pdfunite doesn't support
|
|
|
|
if error.include?(PDFUNITE_ENCRYPTED_PDF_ERROR_STRING)
|
|
|
|
Rails.logger.warn("Cannot merge encrypted PDF #{file.path}, skipping!")
|
|
|
|
|
|
|
|
file.close(true)
|
|
|
|
merged_file.close(true)
|
|
|
|
|
|
|
|
# return the report file unchanged, as no merge was done
|
|
|
|
return report_file
|
|
|
|
elsif !status.success? || !File.file?(merged_file)
|
|
|
|
raise StandardError, "There was an error merging report and PDF file preview (#{error})"
|
2021-04-23 04:28:50 +08:00
|
|
|
end
|
|
|
|
|
2021-05-06 16:53:40 +08:00
|
|
|
file.close(true)
|
|
|
|
report_file.close(true)
|
|
|
|
|
2021-04-23 04:28:50 +08:00
|
|
|
merged_file
|
|
|
|
end
|
2021-05-03 21:27:17 +08:00
|
|
|
|
2023-12-07 17:23:56 +08:00
|
|
|
def prepend_title_page
|
|
|
|
unless File.exist?(Rails.root.join('app', 'views', 'reports', 'templates', @template, 'cover.html.erb'))
|
|
|
|
return @file
|
2021-05-05 21:12:49 +08:00
|
|
|
end
|
|
|
|
|
2021-05-03 21:27:17 +08:00
|
|
|
total_pages = 0
|
|
|
|
|
2023-11-03 20:19:46 +08:00
|
|
|
IO.popen(['pdfinfo', @file.path], 'r+') do |f|
|
2021-05-03 21:27:17 +08:00
|
|
|
total_pages = f.read.split("\n")
|
|
|
|
.find { |i| i.split(':')[0] == 'Pages' }
|
|
|
|
.gsub(/[^0-9]/, '')
|
|
|
|
end
|
|
|
|
|
|
|
|
title_page = Tempfile.new(['title_page', '.pdf'], binmode: true)
|
|
|
|
merged_file = Tempfile.new(['report', '.pdf'], binmode: true)
|
|
|
|
|
2023-11-03 20:19:46 +08:00
|
|
|
title_page_html = @renderer.render_to_string(
|
|
|
|
template: "reports/templates/#{@template}/cover",
|
2023-10-13 18:49:50 +08:00
|
|
|
layout: false,
|
|
|
|
formats: :html,
|
2023-11-03 20:19:46 +08:00
|
|
|
locals: { report: @report, total_pages: total_pages.to_i, logo: report_logo }
|
2021-05-03 21:27:17 +08:00
|
|
|
)
|
|
|
|
|
2023-10-13 18:49:50 +08:00
|
|
|
Grover.new(
|
|
|
|
title_page_html,
|
2023-11-03 20:19:46 +08:00
|
|
|
format: 'A4',
|
|
|
|
style_tag_options: @style_tag_options,
|
|
|
|
script_tag_options: @script_tag_options,
|
|
|
|
emulate_media: 'screen',
|
|
|
|
print_background: true,
|
2023-11-14 16:48:29 +08:00
|
|
|
display_url: Rails.application.routes.default_url_options[:host]
|
2023-10-13 18:49:50 +08:00
|
|
|
).to_pdf(title_page.path)
|
|
|
|
|
2021-05-03 21:27:17 +08:00
|
|
|
title_page.rewind
|
|
|
|
|
|
|
|
success = system(
|
2023-11-03 20:19:46 +08:00
|
|
|
'pdfunite', title_page.path, @file.path, merged_file.path
|
2021-05-03 21:27:17 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
raise StandardError, 'There was an error merging report and title page' unless success && File.file?(merged_file)
|
|
|
|
|
2023-11-03 20:19:46 +08:00
|
|
|
@file.close(true)
|
2021-05-06 16:53:40 +08:00
|
|
|
title_page.close(true)
|
2021-05-03 21:27:17 +08:00
|
|
|
|
|
|
|
merged_file
|
|
|
|
end
|
2021-06-10 21:29:28 +08:00
|
|
|
|
|
|
|
def report_logo
|
2023-08-24 22:27:33 +08:00
|
|
|
'scinote_logo.svg'
|
2021-06-10 21:29:28 +08:00
|
|
|
end
|
|
|
|
|
2023-10-03 15:31:13 +08:00
|
|
|
def build_custom_sanitizer_config
|
|
|
|
sanitizer_config = Constants::INPUT_SANITIZE_CONFIG.deep_dup
|
|
|
|
sanitizer_config[:protocols] = {
|
|
|
|
'a' => { 'href' => ['http', 'https', :relative] },
|
|
|
|
'img' => { 'src' => %w(data) }
|
|
|
|
}
|
|
|
|
sanitizer_config
|
|
|
|
end
|
|
|
|
|
2023-09-06 18:51:34 +08:00
|
|
|
# Overrides method from FailedDeliveryNotifiableJob concern
|
|
|
|
def failed_notification_title
|
|
|
|
I18n.t('projects.reports.index.generation.error_pdf_notification_title')
|
|
|
|
end
|
|
|
|
|
|
|
|
# Overrides method from FailedDeliveryNotifiableJob concern
|
|
|
|
def failed_notification_message
|
|
|
|
report = Report.find_by(id: arguments.first)
|
|
|
|
return '' if report.blank?
|
|
|
|
|
|
|
|
report_path =
|
|
|
|
if report.pdf_file.attached?
|
|
|
|
Rails.application.routes.url_helpers
|
|
|
|
.reports_path(team: report.team.id, preview_report_id: report.id, preview_type: :pdf)
|
|
|
|
else
|
|
|
|
Rails.application.routes.url_helpers.reports_path(team: report.team.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
I18n.t('projects.reports.index.generation.error_notification_message',
|
|
|
|
report_link: "<a href='#{report_path}'>#{escape_input(report.name)}</a>",
|
|
|
|
team_name: escape_input(report.team.name))
|
|
|
|
end
|
2023-11-03 20:19:46 +08:00
|
|
|
|
|
|
|
def fetch_asset_content(asset_name)
|
|
|
|
Rails.application
|
|
|
|
.assets_manifest
|
|
|
|
.find_sources(asset_name)
|
|
|
|
.first
|
|
|
|
.to_s
|
|
|
|
.force_encoding(Encoding::UTF_8)
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_margins_from_header
|
|
|
|
header_file_path = Rails.root.join('app', 'views', 'reports', 'templates', @template, 'header.html.erb')
|
|
|
|
return nil unless header_file_path.exist?
|
|
|
|
|
|
|
|
content = File.read(header_file_path)
|
|
|
|
|
|
|
|
margin_comment = content.match(/<!--\s*margins:(.*)\s*-->/)
|
|
|
|
return nil unless margin_comment
|
|
|
|
|
|
|
|
margins = {}
|
|
|
|
margin_comment[1].split(',').each do |margin_pair|
|
|
|
|
key, value = margin_pair.split(':').map(&:strip)
|
|
|
|
margins[key.to_sym] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
margins
|
|
|
|
end
|
|
|
|
|
2024-04-09 15:25:47 +08:00
|
|
|
def cover_pages_count
|
2023-11-03 20:19:46 +08:00
|
|
|
cover_file_path = Rails.root.join('app', 'views', 'reports', 'templates', @template, 'cover.html.erb')
|
2024-04-09 15:25:47 +08:00
|
|
|
return 0 unless cover_file_path.exist?
|
2023-11-03 20:19:46 +08:00
|
|
|
|
|
|
|
content = File.read(cover_file_path)
|
|
|
|
|
|
|
|
cover_pages_comment = content.match(/<!--\s*cover_pages_count:(\d+)\s*-->/)
|
2024-04-09 15:25:47 +08:00
|
|
|
return 1 unless cover_pages_comment
|
2023-11-03 20:19:46 +08:00
|
|
|
|
2024-04-09 15:25:47 +08:00
|
|
|
cover_pages_comment[1].to_i
|
2023-11-03 20:19:46 +08:00
|
|
|
end
|
2021-04-06 19:56:24 +08:00
|
|
|
end
|
|
|
|
end
|