Skip to content

Commit

Permalink
Generate a preview without print margins
Browse files Browse the repository at this point in the history
When a PDF is used for both printing and displaying. It will most likely
contain a crop box in order to hide print margins when displaying the PDF.

Use Poppler's parameter to automatically use the crop box (visible box)
rather than the media box (printable box) in order to remove those margins
when drawing the PDF.

See https://manpages.debian.org/testing/poppler-utils/pdftoppm.1.en.html
  • Loading branch information
genezys authored and georgeclaghorn committed Sep 18, 2020
1 parent 6d8ed35 commit 5d0e5f0
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 12 deletions.
7 changes: 7 additions & 0 deletions activestorage/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
* The Poppler PDF previewer renders a preview image using the original
document's crop box rather than its media box, hiding print margins. This
matches the behavior of the MuPDF previewer.

*Vincent Robert*


## Rails 5.2.4.4 (September 09, 2020) ##

* No changes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def preview

private
def draw_first_page_from(file, &block)
# use 72 dpi to match thumbnail dimesions of the PDF
draw self.class.pdftoppm_path, "-singlefile", "-r", "72", "-png", file.path, &block
# use 72 dpi to match thumbnail dimensions of the PDF
draw self.class.pdftoppm_path, "-singlefile", "-cropbox", "-r", "72", "-png", file.path, &block
end
end
end
Binary file added activestorage/test/fixtures/files/cropped.pdf
Binary file not shown.
13 changes: 13 additions & 0 deletions activestorage/test/models/preview_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ class ActiveStorage::PreviewTest < ActiveSupport::TestCase
assert_equal 792, image.height
end

test "previewing a cropped PDF" do
blob = create_file_blob(filename: "cropped.pdf", content_type: "application/pdf")
preview = blob.preview(resize: "640x280").processed

assert_predicate preview.image, :attached?
assert_equal "cropped.png", preview.image.filename.to_s
assert_equal "image/png", preview.image.content_type

image = read_image(preview.image)
assert_equal 430, image.width
assert_equal 145, image.height
end

test "previewing an MP4 video" do
blob = create_file_blob(filename: "video.mp4", content_type: "video/mp4")
preview = blob.preview(resize: "640x280").processed
Expand Down
21 changes: 16 additions & 5 deletions activestorage/test/previewer/mupdf_previewer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
require "active_storage/previewer/mupdf_previewer"

class ActiveStorage::Previewer::MuPDFPreviewerTest < ActiveSupport::TestCase
setup do
@blob = create_file_blob(filename: "report.pdf", content_type: "application/pdf")
end

test "previewing a PDF document" do
ActiveStorage::Previewer::MuPDFPreviewer.new(@blob).preview do |attachable|
blob = create_file_blob(filename: "report.pdf", content_type: "application/pdf")

ActiveStorage::Previewer::MuPDFPreviewer.new(blob).preview do |attachable|
assert_equal "image/png", attachable[:content_type]
assert_equal "report.png", attachable[:filename]

Expand All @@ -20,4 +18,17 @@ class ActiveStorage::Previewer::MuPDFPreviewerTest < ActiveSupport::TestCase
assert_equal 792, image.height
end
end

test "previewing a cropped PDF document" do
blob = create_file_blob(filename: "cropped.pdf", content_type: "application/pdf")

ActiveStorage::Previewer::MuPDFPreviewer.new(blob).preview do |attachable|
assert_equal "image/png", attachable[:content_type]
assert_equal "cropped.png", attachable[:filename]

image = MiniMagick::Image.read(attachable[:io])
assert_equal 430, image.width
assert_equal 145, image.height
end
end
end
21 changes: 16 additions & 5 deletions activestorage/test/previewer/poppler_pdf_previewer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
require "active_storage/previewer/poppler_pdf_previewer"

class ActiveStorage::Previewer::PopplerPDFPreviewerTest < ActiveSupport::TestCase
setup do
@blob = create_file_blob(filename: "report.pdf", content_type: "application/pdf")
end

test "previewing a PDF document" do
ActiveStorage::Previewer::PopplerPDFPreviewer.new(@blob).preview do |attachable|
blob = create_file_blob(filename: "report.pdf", content_type: "application/pdf")

ActiveStorage::Previewer::PopplerPDFPreviewer.new(blob).preview do |attachable|
assert_equal "image/png", attachable[:content_type]
assert_equal "report.png", attachable[:filename]

Expand All @@ -20,4 +18,17 @@ class ActiveStorage::Previewer::PopplerPDFPreviewerTest < ActiveSupport::TestCas
assert_equal 792, image.height
end
end

test "previewing a cropped PDF document" do
blob = create_file_blob(filename: "cropped.pdf", content_type: "application/pdf")

ActiveStorage::Previewer::PopplerPDFPreviewer.new(blob).preview do |attachable|
assert_equal "image/png", attachable[:content_type]
assert_equal "cropped.png", attachable[:filename]

image = MiniMagick::Image.read(attachable[:io])
assert_equal 430, image.width
assert_equal 145, image.height
end
end
end

0 comments on commit 5d0e5f0

Please sign in to comment.