From 3803671a816232395f538c61046b00c875c1444b Mon Sep 17 00:00:00 2001 From: Vincent Robert Date: Wed, 16 Sep 2020 16:18:53 +0200 Subject: [PATCH] Generate a preview without print margins 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 --- activestorage/CHANGELOG.md | 6 +++++ .../previewer/poppler_pdf_previewer.rb | 2 +- activestorage/test/fixtures/files/cropped.pdf | Bin 0 -> 14127 bytes activestorage/test/models/preview_test.rb | 13 +++++++++++ .../test/previewer/mupdf_previewer_test.rb | 21 +++++++++++++----- .../previewer/poppler_pdf_previewer_test.rb | 21 +++++++++++++----- 6 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 activestorage/test/fixtures/files/cropped.pdf diff --git a/activestorage/CHANGELOG.md b/activestorage/CHANGELOG.md index 4a91a8e070e..51fe89c8296 100644 --- a/activestorage/CHANGELOG.md +++ b/activestorage/CHANGELOG.md @@ -1,3 +1,9 @@ +* 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* + * Touch parent model when an attachment is purged. *Víctor Pérez Rodríguez* diff --git a/activestorage/lib/active_storage/previewer/poppler_pdf_previewer.rb b/activestorage/lib/active_storage/previewer/poppler_pdf_previewer.rb index c7f9136983d..8eaffc64914 100644 --- a/activestorage/lib/active_storage/previewer/poppler_pdf_previewer.rb +++ b/activestorage/lib/active_storage/previewer/poppler_pdf_previewer.rb @@ -29,7 +29,7 @@ module ActiveStorage private def draw_first_page_from(file, &block) # use 72 dpi to match thumbnail dimensions of the PDF - draw self.class.pdftoppm_path, "-singlefile", "-r", "72", "-png", file.path, &block + draw self.class.pdftoppm_path, "-singlefile", "-cropbox", "-r", "72", "-png", file.path, &block end end end diff --git a/activestorage/test/fixtures/files/cropped.pdf b/activestorage/test/fixtures/files/cropped.pdf new file mode 100644 index 0000000000000000000000000000000000000000..8a54a1008df20ac78bcc95ad9fd68f1e87c35a87 GIT binary patch literal 14127 zcmbt*1yq!6&^EEs(wz&^xyu4ecXxM4cXx+`gc8!ypooC9ARQ6{(%py%NF$y9;;V0d zf1LC0;qdIv-ZgVyGyB~0TtlZSDa{6EhoI4YTt8ahF8+}>^05bv3kU)_o7tfW3IaJ4 zEuE}AY=GP_kvfn=+Sb9t(hYWXF!ivMv^00NumlPVqq%#yS(-Yc`M|SiqG&Q|g19&} zK|GL2h!%(ks#*!sW|Y@L(dp9|!IEW@S7u{mQW{ia><2Idl<-i5VVzoSl5 z5FV)%g%c0MyFjyaviLP9?A`rZ?iX->g>v10cmIy_Z>!Ywb+H6;sG3^;{O@MzNF$gK70^ zp8I~kt)c4XY_4JH0W^S3CMgZ%kaL7hDgNs!@#`uFGy-zys+ieXntK56Z7Amm210+% zCM~6Jls5CmXL6EaCXyhF*UaY^8GQehLWkfJrI1aDNJ>7TMu_tOE(E; zM;B+Odz6)C`!X9hV!tvnlZ18oSIk_5%p6-IQa{_B+PPSbtwZluC~N5K$wT@Jeh;HfArP9NnLOpomPEr}JsFRU`BbE=T<@wDzG zY?2`;fHYC#E4_=8k4gqdiq=o;ktUU4T>#iK%Rzo^TXE$gQ+j|fjUFu*HqVU=l**Tx zqJXC%*8uf8!{@8`N7*B?Xd30}SL@Z!ioe!J+0i0M0?Po^25k?ka(H{iN2*vCKkY=0 zn_7wzF(62a@*rfeF(@F3Cm!*#L@SUyY=2+B&N5q85Oc2yUT9p;>R1Oz0j&b`3UMJP zaut-AH-HOI14IlRCVSqM=v(|e9AWN>_NT{WT4Jz8_Mz^)Ba6#o?A99@d23XMNWCFO zbiV)~g5?u(9&3~5Z#d`PU-gNK5{`X>X&Rm&dZG5gdJxG}Twzv?y}Tb+oY{kkMig*O ztIF>|A&Z8d<z~>kdeP;u^_y1-zz#-wn%87SJM z3ZMw*%{9&S6XAr!yIMOc_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_|hqD_R_!ry%=VzF8CGU?TKbL-A@8E3lkIxF0zTVDmuzlqG^^{mlOjVKp zw-k-@kInt7YQe|N(h3a(xVa(RKr7%MGB76(6t+wEFQC&e83+XCg{}J^ zGB7769}Il|kikCOPx&ty%*Owbaq|7Q9fTLA#6SCTgScT}{D%xSFE`Al|B!*WLC}A< z1MzXgH27CLKJI%%{i82zJjlQG1#?3FV*@ZJ7uUbXgUNXQy%sPh_x%?BYYuL%|3Sv{ zUz>9O$1bq_fc#rGn2h^xw)b!|wRNy``&sg%Ve1Ej3+ybb?(7V6^Ltl@;YQBM${FVK zf4rB3dA%SMYQ@9JXKrr6Ys$+BHiPn*nsV{-K{?HNIKfbJh@~*v|Gf#UROV-O3CweU z0t?K^%MC`OqmxpRM!TQv-h=KP<2N4vLg_z!;~$^}{sJwB=0Du&m;a#s_MiXJJ%0HR z@V5{B>12$Krsl9{^E+U{Vh)&_od;;hXzK26>1gI~UwZ`Rli>t$f(@De8;e+hVlFPQ z!XKFX!P>Ednb7|9#GkQ*69nRep_~r{<>dPvOa2xRe}|5L#^}F?Xqd77EMK}WK>Bat zlbf^4&pIa{FO(eyFbEV40^w!{fq~p$C_97;=D=J~K6WT9n%sAH_PCE4uo5Fx)4$hK z{i}THKXKvz6c!*{JpX|(1Pb{Z{QnL^IT*-b0Fs7bOhQ7ER}6+RaVU?N*iVc}++$1v zA|)&+Dai%l;gja$l;9TQg-SrdQe5KVl3bh;P+lG$9w~_MAKLs4$$#Qo74~BQ`!$3i X?jP`m6=}iho&H25gqM%+7b5=`Zb+nG literal 0 HcmV?d00001 diff --git a/activestorage/test/models/preview_test.rb b/activestorage/test/models/preview_test.rb index 87d2b6b0ec9..90e5b092faa 100644 --- a/activestorage/test/models/preview_test.rb +++ b/activestorage/test/models/preview_test.rb @@ -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 diff --git a/activestorage/test/previewer/mupdf_previewer_test.rb b/activestorage/test/previewer/mupdf_previewer_test.rb index 6c2db6fcbf4..65869dfe37c 100644 --- a/activestorage/test/previewer/mupdf_previewer_test.rb +++ b/activestorage/test/previewer/mupdf_previewer_test.rb @@ -6,12 +6,10 @@ require "database/setup" 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] @@ -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 diff --git a/activestorage/test/previewer/poppler_pdf_previewer_test.rb b/activestorage/test/previewer/poppler_pdf_previewer_test.rb index 2b41c8b642a..5809b5a0589 100644 --- a/activestorage/test/previewer/poppler_pdf_previewer_test.rb +++ b/activestorage/test/previewer/poppler_pdf_previewer_test.rb @@ -6,12 +6,10 @@ require "database/setup" 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] @@ -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