Skip to content

Commit

Permalink
Reduce Memory Usage (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
JensRavens authored May 14, 2024
1 parent 45277e7 commit 079618e
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 98 deletions.
23 changes: 10 additions & 13 deletions lib/pixelpress/document.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
require_relative 'fake_file'

module Pixelpress
class Document
attr_reader :html
attr_reader :file_name
attr_reader :filename

def initialize(html, renderer, options = {})
@html = html
def initialize(file, renderer, options = {})
@file = file
@renderer = renderer
@file_name = options[:file_name]
@filename = options[:filename]
end

def html
file.read
end

def pdf
FakeFile.new pdf_data, original_filename: file_name
@pdf ||= renderer.render(file)
end

private

attr_accessor :renderer

def pdf_data
@pdf_data ||= renderer.render(html)
end
attr_accessor :renderer, :file
end
end
19 changes: 0 additions & 19 deletions lib/pixelpress/fake_file.rb

This file was deleted.

4 changes: 2 additions & 2 deletions lib/pixelpress/renderers/test_renderer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Pixelpress::TestRenderer
def render(html)
File.binread(File.join(__dir__, "test.pdf"))
def render(_)
File.open(File.join(__dir__, "test.pdf"))
end
end
19 changes: 4 additions & 15 deletions lib/pixelpress/renderers/weasyprint_renderer.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
class Pixelpress::WeasyPrintRenderer
def render(html)
def render(input)
output = Tempfile.new

command = "#{executable_path} --encoding UTF-8 - -"

error = nil
result = nil
process = nil
Open3.popen3(command) do |stdin, stdout, stderr, thread|
stdin.puts html
stdin.close_write
result = stdout.read
error = stderr.read
process = thread.value
end
raise "command failed (exitstatus=#{result.exitstatus}): #{command}\n #{error}" unless process.success?
return result
system executable_path, input.path, output.path, exception: true
return output
end

def version
Expand Down
7 changes: 6 additions & 1 deletion lib/pixelpress/rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ module Rendering
class_attribute :renderer, default: WeasyPrintRenderer.new

def document
@document ||= Document.new render_to_string(template), renderer, file_name: try(:file_name)
@document ||= begin
file = Tempfile.new
file.write render_to_string(template)
file.rewind
Document.new file, renderer, filename: (try(:filename) || try(:file_name))
end
end

protected
Expand Down
31 changes: 31 additions & 0 deletions spec/models/document_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'spec_helper'

describe Pixelpress do
let(:renderer) { Pixelpress::TestRenderer.new }
let(:document) do
file = Tempfile.new
file.write <<~HTML
<html>
<body>
<h1>Invoice</h1>
</body>
</html>
HTML
file.rewind
Pixelpress::Document.new(file, renderer, filename: 'invoice-1')
end

it 'checks if the file name of pdf is correct' do
expect(document.filename).to eq 'invoice-1'
end

it 'checks if it is not calling weasyprint when html is called' do
expect(renderer).not_to receive(:render)
document.html
end

it 'checks if it is calling weasydocument when pdf is called' do
expect(renderer).to receive(:render)
document.pdf
end
end
28 changes: 28 additions & 0 deletions spec/models/printer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'spec_helper'

describe Pixelpress::Base do
let(:renderer) { Pixelpress::TestRenderer.new }
let(:document) { InvoicePrinter.invoice }

before(:each) do
ActionController::Base.append_view_path File.join(spec_root)
InvoicePrinter.renderer = renderer
end

class InvoicePrinter < Pixelpress::Base
def invoice
end

def filename
'sasha'
end
end

it 'selects the right template' do
expect(document.html).to include 'Invoice'
end

it 'checks if the file name of pdf is correct' do
expect(document.filename).to eq 'sasha'
end
end
47 changes: 0 additions & 47 deletions spec/pixelpress_spec.rb

This file was deleted.

Binary file removed spec/printers/invoice/sasha.pdf
Binary file not shown.
8 changes: 7 additions & 1 deletion spec/renderers/weasyprint_renderer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@

describe Pixelpress::WeasyPrintRenderer do
let(:renderer) { Pixelpress::WeasyPrintRenderer.new }
let (:input) do
file = Tempfile.new
file.write("<html><body><h1>hello</h1></body></html>")
file.rewind
file
end

it "should render html to pdf" do
expect(renderer.render("<html><body><h1>hello</h1></body></html>")).to start_with("%PDF")
expect(renderer.render(input).read).to start_with("%PDF")
end
end

0 comments on commit 079618e

Please sign in to comment.