PDF clipping on the wrong box?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
AnrDaemon
Posts: 11
Joined: 2016-02-10T10:17:59-07:00
Authentication code: 1151

PDF clipping on the wrong box?

Post by AnrDaemon »

I'm preparing some documents for translation, and came around an issue.
This one: http://www.komyokk.co.jp/pdata/tpdf/104U_1.pdf
When you look it in the PDF viewer, it correctly shows page boundary as printed.
But when you render it with imagick, it adds huge margins to the resulting image.

I feel like there's a simple solution I'm overlooking.

The command I'm using is:

Code: Select all

convert -density 600 pdf:"$1" -background white -alpha opaque png48:"${1%.*}.png"
Tried to add +repage, but that did not change anything.
$ convert -version
Version: ImageMagick 6.9.9-11 Q16 x86_64 2017-09-30 http://www.imagemagick.org
Copyright: © 1999-2017 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC OpenMP
Delegates (built-in): autotrace bzlib cairo fftw fontconfig fpx freetype gslib jbig jng jp2 jpeg lcms lzma pangocairo png ps rsvg tiff webp x xml zlib
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: PDF clipping on the wrong box?

Post by snibgo »

If you look at the PDF file in a plain text editor, you will see it has a media box and also a smaller cropbox. By default IM ignores the cropbox, but can be told to use it:

Code: Select all

convert -define pdf:use-cropbox=true 104U_1.pdf -background pink -layers flatten out.png
I flatten against a colour so I can more easily see the boundary.
snibgo's IM pages: im.snibgo.com
AnrDaemon
Posts: 11
Joined: 2016-02-10T10:17:59-07:00
Authentication code: 1151

Re: PDF clipping on the wrong box?

Post by AnrDaemon »

Worked like a charm! Thank you!
AnrDaemon
Posts: 11
Joined: 2016-02-10T10:17:59-07:00
Authentication code: 1151

Re: PDF clipping on the wrong box?

Post by AnrDaemon »

I'm back looking for help. I want to extract tube image, and I've basically done so, but I came across an issue.

Code: Select all

convert -density 300 -define pdf:use-cropbox=true pdf:"104U_1.pdf" -background white -alpha on -layers flatten -resize "1366x>" -gravity north -crop 1024x180+0+180 png48:"104U.pdf"
Does almost what I want, but it removes transparency present in the original document.
The key point seems to be the "-layers flatten", but when I remove it, -resize does not work either.
I've also tried -layers merge and various -background settings, but that randomly removes layers from resulting image and/or making image turn black'ish in some viewers.
Is there a solution?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: PDF clipping on the wrong box?

Post by snibgo »

AnrDaemon wrote:The key point seems to be the "-layers flatten", but when I remove it, -resize does not work either.
In what way doesn't "-resize" work? It works fine for me.

Your output is:

Code: Select all

png48:"104U.pdf"
This creates a PNG format file with a filename that ends with ".pdf", which seems a bad idea. Why do you do this? It will confuse other software, for example Adobe Reader thinks it is a corrupt PDF file.
snibgo's IM pages: im.snibgo.com
AnrDaemon
Posts: 11
Joined: 2016-02-10T10:17:59-07:00
Authentication code: 1151

Re: PDF clipping on the wrong box?

Post by AnrDaemon »

snibgo wrote: 2018-05-16T08:28:27-07:00
AnrDaemon wrote:The key point seems to be the "-layers flatten", but when I remove it, -resize does not work either.
In what way doesn't "-resize" work? It works fine for me.
If you drop cropping, and see the resulting image dimensions, they are 1700-something wide.
Your output is:

Code: Select all

png48:"104U.pdf"
This creates a PNG format file with a filename that ends with ".pdf", which seems a bad idea. Why do you do this? It will confuse other software, for example Adobe Reader thinks it is a corrupt PDF file.
Sorry, that was a typo. Indeed, in the actual code it was .png.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: PDF clipping on the wrong box?

Post by snibgo »

Testing with With IM v6.9.5-3 and v7.0.7-28, I get the same size whether I flatten or not:

Code: Select all

f:\web\im>%IM%convert -density 300 -define pdf:use-cropbox=true 104U_1.pdf -resize "1366x>" info:

104U_1.pdf PDF 1366x1938 1366x1938+0+0 16-bit sRGB 0.969u 0:00.125

f:\web\im>%IM%convert -density 300 -define pdf:use-cropbox=true 104U_1.pdf -background white -alpha on -layers flatten -resize "1366x>" info:

104U_1.pdf PDF 1366x1938 1366x1938+0+0 16-bit sRGB 0.875u 0:00.110
snibgo's IM pages: im.snibgo.com
AnrDaemon
Posts: 11
Joined: 2016-02-10T10:17:59-07:00
Authentication code: 1151

Re: PDF clipping on the wrong box?

Post by AnrDaemon »

Ok, what's wrong with this code then?

Code: Select all

+ convert -density 300 -define pdf:use-cropbox=true pdf:104U_1.pdf +background +alpha -resize '1366x>' info:
pdf:104U_1.pdf PDF 1747x2479 1747x2479+0+0 16-bit sRGB 176138B 0.078u 0:00.084
+ convert -version
Version: ImageMagick 6.9.9-11 Q16 x86_64 2017-09-30 http://www.imagemagick.org
Copyright: c 1999-2017 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC OpenMP
Delegates (built-in): autotrace bzlib cairo fftw fontconfig fpx freetype gslib jbig jng jp2 jpeg lcms lzma pangocairo png ps rsvg tiff webp x xml zlib
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: PDF clipping on the wrong box?

Post by snibgo »

If you want to write to two or more outputs, such as info: and a file, you need to use the "+write" or "-write" command for all except the last, like this:

Code: Select all

convert -density 300 -define pdf:use-cropbox=true pdf:104U_1.pdf +background +alpha -resize '1366x>' +write info: pdf:104U_1.pdf
The size of the resulting PDF may not match the "info:" because PDF is a vector format and the size in pixels depends on the resolution ("-density").
snibgo's IM pages: im.snibgo.com
AnrDaemon
Posts: 11
Joined: 2016-02-10T10:17:59-07:00
Authentication code: 1151

Re: PDF clipping on the wrong box?

Post by AnrDaemon »

That's not quite the question I have.
In the example above, I've replaced the png:… with info: for illustrative purposes. The saved PNG do match the dimensions shown in text output.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: PDF clipping on the wrong box?

Post by snibgo »

You have "+alpha +background". What do they do? I don't see them in the documentation http://www.imagemagick.org/script/comma ... background
snibgo's IM pages: im.snibgo.com
AnrDaemon
Posts: 11
Joined: 2016-02-10T10:17:59-07:00
Authentication code: 1151

Re: PDF clipping on the wrong box?

Post by AnrDaemon »

+option tells imagick to use default value for the option. I.e. +repage is a shortcut for explicit default of "0x0+0+0".
The default background color (if none is specified or found in the image) is white.
However, I can't find the mention of default for -alpha. And that seems to be what throw it off.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: PDF clipping on the wrong box?

Post by snibgo »

The "-background" setting does have a default. The undocumented "+background" sets this.

But "-alpha" isn't a setting, and a default doesn't make sense. Sadly IM doesn't check the sign, so takes whatever follows as the argument to "-alpha" and either throws an error or gets confused.
snibgo's IM pages: im.snibgo.com
AnrDaemon
Posts: 11
Joined: 2016-02-10T10:17:59-07:00
Authentication code: 1151

Re: PDF clipping on the wrong box?

Post by AnrDaemon »

That explains the behavior. Now, what about my question? Is there a way to preserve alpha while merging layers?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: PDF clipping on the wrong box?

Post by snibgo »

AnrDaemon wrote:Is there a way to preserve alpha while merging layers?
I thought you didn't want to merge layers? There is only one image, so the only point in merging layers is to flatten the image against a background, which removes alpha, and you don't want that.

How about:

Code: Select all

convert -density 300 -define pdf:use-cropbox=true pdf:"104U_1.pdf" -resize "1366x>" -gravity north -crop 1024x180+0+180 +repage 104U.png
snibgo's IM pages: im.snibgo.com
Post Reply