Skip to content

[Improvement]: Add -dUseCropBox option in Ghostscript to generation of thumbnail from pdf (#18317)#18317

Merged
kingjia90 merged 19 commits intopimcore:12.xfrom
croitoruion:fix-for-pdf-thumbnail-generation
Nov 5, 2025
Merged

[Improvement]: Add -dUseCropBox option in Ghostscript to generation of thumbnail from pdf (#18317)#18317
kingjia90 merged 19 commits intopimcore:12.xfrom
croitoruion:fix-for-pdf-thumbnail-generation

Conversation

@croitoruion
Copy link
Copy Markdown
Contributor

@croitoruion croitoruion commented Apr 16, 2025

Changes in this pull request

Resolves #
This PR updates the Ghostscript command used for generating thumbnails from PDFs by adding the -dUseCropBox flag. This ensures that only the visible content area (as defined by the PDF's CropBox) is rendered, avoiding excessive white margins around elements like single images or centered content.

  • Effect: If there is no CropBox defined in the PDF, Ghostscript will fall back to using the MediaBox. The MediaBox typically includes the entire page, including any extra margins or non-content areas.
  • This is useful when the PDF has extra whitespace or content outside the defined printable area, and you want to focus on the main content

When a PDF page contains only a small image or centered content, the default rendering includes large blank margins, resulting in thumbnails where the actual content appears tiny. (Attaching examples: the generated thumbnail and its usage to clearly demonstrate the issue.)
image

image

Additional info

After we set the flag we obtain such image
image

Attaching also original pdf

osteoporosis_diagram.pdf

@github-actions
Copy link
Copy Markdown

Review Checklist

  • Target branch (11.5 for bug fixes, others 11.x)
  • Tests (if it's testable code, there should be a test for it - get help)
  • Docs (every functionality needs to be documented, see here)
  • Migration incl. install.sql (e.g. if the database schema changes, ...)
  • Upgrade notes (deprecations, important information, migration hints, ...)
  • Label
  • Milestone

@sonarqubecloud
Copy link
Copy Markdown

@croitoruion croitoruion changed the title Use -dUseCropBox in Ghostscript to improve thumbnail cropping Use -dUseCropBox in Ghostscript to improve generation of thumbnail from pdf Apr 23, 2025
@ghost ghost added the Pimcore:ToDo label Apr 28, 2025
@kingjia90 kingjia90 changed the base branch from 11.5 to 12.x August 13, 2025 08:44
@kingjia90
Copy link
Copy Markdown
Contributor

kingjia90 commented Aug 13, 2025

Thank you for your PR!
Not sure whether it should be seen as a bug or could be added as an improvement, wdyt?
If it's an improvement, maybe let's add an entry in the upgrade note

@kingjia90 kingjia90 self-assigned this Aug 13, 2025
@brusch
Copy link
Copy Markdown
Member

brusch commented Aug 14, 2025

I'm afraid that we can't just change the default to use CropBox instead of MediaBox, because there are also use-cases in Pimcore where you might want MediaBox, e.g. when using the PDF editable: https://docs.pimcore.com/platform/Pimcore/Documents/Editables/PDF/

While I agree, that Cropbox would make sense in certain cases for the preview images in the admin UI (maybe also not for all 🤔) it could lead to bad presentations in many other cases.

So actually I'm thinking of making it somehow individually configurable, maybe on the image thumbnail configuration as an advanced option, same as Rasterize SVGs (Imagick)?
Then you could also use that for custom thumbnail config for the previews.

@kingjia90 kingjia90 marked this pull request as draft August 14, 2025 08:50
@kingjia90
Copy link
Copy Markdown
Contributor

Agree with Bernie, i am changing base branch to 12.x,adding Improvement label and switch to draft, looking forward to the adaptation to make this cropbox configurable, TIA

@croitoruion
Copy link
Copy Markdown
Contributor Author

croitoruion commented Aug 14, 2025

Agree, good idea, to add this logic as parameter in thumbnails configuration.

@kingjia90
Copy link
Copy Markdown
Contributor

@croitoruion 🏓 Any updates on this? Thank you in advance

@kingjia90
Copy link
Copy Markdown
Contributor

@croitoruion any chance to apply the configuration change? TIA

@croitoruion
Copy link
Copy Markdown
Contributor Author

@kingjia90 Sorry, I haven’t had time for that, and I don’t think I’ll be able to in the near future.

@kingjia90
Copy link
Copy Markdown
Contributor

All good, thank you for letting me know.

If you don't mind, i'll take it over and try to finalize it.

@kingjia90 kingjia90 added this to the 12.3.0 milestone Sep 29, 2025
…pdf-thumbnail-generation

# Conflicts:
#	models/Asset/Image/Thumbnail/Config/Dao.php
@kingjia90 kingjia90 self-requested a review October 2, 2025 13:40
@kingjia90 kingjia90 marked this pull request as ready for review October 15, 2025 07:26
@kingjia90

This comment was marked as off-topic.

@kingjia90

This comment was marked as off-topic.

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Nov 5, 2025

@kingjia90 kingjia90 merged commit a75e0e8 into pimcore:12.x Nov 5, 2025
14 of 20 checks passed
@kingjia90 kingjia90 changed the title Use -dUseCropBox in Ghostscript to improve generation of thumbnail from pdf [Improvement]: Add -dUseCropBox option in Ghostscript to generation of thumbnail from pdf (#18317) Nov 5, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Nov 5, 2025
@kingjia90
Copy link
Copy Markdown
Contributor

kingjia90 commented Nov 5, 2025

Finalized and merged.
Thank you again for reporting and the baseline PR!

Internal notes: codeception tests appear failing but actually is somehow running in pair, one series is failing due to the secret not working on forks, the other is all green. We will investigate further on this separately
image

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants