Skip to content

Instantly share code, notes, and snippets.

# frozen_string_literal: true
module Templates::Templates::Contexts::PdfGeneration::Strategies
# The HexaPdf class is responsible for generating HTML content from a template
# using the HexaPDF library. It processes the template instructions and converts
# them into an HTML representation suitable for PDF generation.
class HexaPdf < BaseService
# Generates HTML content from a template using HexaPDF.
#
# @param [Object] template The template object containing instructions.
# frozen_string_literal: true
# The Base class serves as a foundational class for key type applicators.
# It defines the common interface and behavior for specific key type implementations.
class Templates::Components::KeyTypeApplicators::Base
attr_reader :current_value
# Applies the key type applicator to the provided arguments.
#
# @param [Templates::Component] template_component The component to be applied.
# frozen_string_literal: true
# PdfMailer is responsible for sending emails that contain PDF attachments.
# It formats the email using the user's details and attaches the generated PDF.
class PdfMailer < ApplicationMailer
# Sends an email with the generated PDF attachment.
#
# @param input [Hash] A hash containing input data including:
# - :user [User] The user receiving the email.
# - :template [Templates::Template] The template associated with the PDF.
# frozen_string_literal: true
# The EmailDispatch class is responsible for dispatching emails containing generated PDFs.
# It uses a specified PDF generation strategy and the associated template to create the PDF,
# then sends it to the user via email.
class Templates::Templates::Contexts::EmailDispatch < BaseService
MailerInput = Struct.new(:user, :tmpfile, :template, keyword_init: true)
performs_checks
# Initializes a new instance of the EmailDispatch class.
# frozen_string_literal: true
class Templates::TemplatesController < ApplicationController
before_action :ensure_current_user!
def dispatch_mail
context = ::Templates::Templates::Contexts::EmailDispatch.call(input: email_dispatch_input)
return error_response(context.message) unless context.success?
render json: ::Templates::TemplateBlueprint.render(context.payload), status: :created
# frozen_string_literal: true
module Templates
module Components
module KeyTypeApplicators
class TotalApplicator < ::Templates::Components::KeyTypeApplicators::Base
def apply!
selector.content = computed_value if apply_changes?
document
end
# frozen_string_literal: true
# The Engine class is responsible for generating PDFs based on a specified strategy
# and a given template ID. It modifies the original HTML document by applying
# components from the template before generating the final PDF file.
#
# Supported strategies for PDF generation include :pdfkit and :hexapdf.
class Templates::Templates::Contexts::PdfGeneration::Service < BaseService
HTML_GENERATION_STRATEGIES = %i[pdfkit hexapdf].freeze
# frozen_string_literal: true
# DEPRECATED
module PDF
module Pages
module HexaDefinitions
module GraphicDefinitions
def close_fill_stroke(instruction, page)
page.canvas.close_fill_stroke(*instruction[:args])
end
[{:name=>:set_graphics_state_parameters, :args=>[:GSa]},
{:name=>:set_nonstroke_color_space, :args=>[:CSp]},
{:name=>:set_stroke_color_space, :args=>[:CSp]},
{:name=>:concatenate_matrix, :args=>[0.24, 0, 0, -0.24, 28.32, 707.039999]},
{:name=>:save_graphics_state, :args=>[]},
{:name=>:save_graphics_state, :args=>[]},
{:name=>:restore_graphics_state, :args=>[]},
{:name=>:restore_graphics_state, :args=>[]},
{:name=>:save_graphics_state, :args=>[]},
{:name=>:save_graphics_state, :args=>[]},
# frozen_string_literal: true
class Templates::ComponentsController < ApplicationController
before_action :ensure_current_user!
def create
ctx = ::Templates::Components::Contexts::Creation.call(input: component_create_input)
return error_response(ctx.message) unless ctx.success?
render json: ::Templates::ComponentBlueprint.render(ctx.payload), status: :created