fulton-digital / pdf-compressor

0 stars 0 forks source link

Use first page aspect ratio as default instead of 16:9 #11

Open LukeL99 opened 6 years ago

LukeL99 commented 6 years ago

For cross-platform compatibility implement this without introducing additional dependencies outside of ghostscript.

Output of:

gs -dNOPAUSE -dBATCH -sDEVICE=bbox -dLastPage=1 -r100 ~/Downloads/test.pdf

Gives:

Processing pages 1 through 1.
Page 1
%%BoundingBox: 164 87 1921 1081
%%HiResBoundingBox: 164.880006 87.840003 1920.240073 1080.000041

With the bounding box coordinates in the format:

lower_left_x lower_left_y upper_right_x upper_right_y

So aspect ratio would be:

(upper_right_x - lower_left_x) / (upper_right_y - lower_left_y)
LukeL99 commented 6 years ago

Could probably increase accuracy through averaging aspect ratios of all pages. Reducing resolution (-r flag) drastically increases processing speed.

gs -dNOPAUSE -dBATCH -sDEVICE=bbox -r10 ~/Downloads/test.pdf | grep -i 'bounding'

%%BoundingBox: 143 79 1923 1080
%%HiResBoundingBox: 143.999993 79.199996 1922.399908 1079.999949
%%BoundingBox: 143 79 1923 1080
%%HiResBoundingBox: 143.999993 79.199996 1922.399908 1079.999949
%%BoundingBox: -4 0 5764 3248
%%HiResBoundingBox: -3.571875 0.000000 5763.599725 3247.199845
%%BoundingBox: 43 107 1750 1080
%%HiResBoundingBox: 43.199998 107.999995 1749.599917 1079.999949
%%BoundingBox: 7 251 5760 3240
%%HiResBoundingBox: 7.200000 251.999988 5759.999725 3239.999846
%%BoundingBox: 43 0 1923 1080
%%HiResBoundingBox: 43.199998 0.000000 1922.399908 1079.999949
%%BoundingBox: 129 0 5760 3240
%%HiResBoundingBox: 129.599994 0.000000 5759.999725 3239.999846
%%BoundingBox: 129 230 5429 3111
%%HiResBoundingBox: 129.599994 230.399989 5428.799741 3110.399852
%%BoundingBox: 129 230 5429 3111
%%HiResBoundingBox: 129.599994 230.399989 5428.799741 3110.399852
%%BoundingBox: 43 93 1721 1037
%%HiResBoundingBox: 43.199998 93.599996 1720.799918 1036.799951
%%BoundingBox: 86 0 3788 2168
%%HiResBoundingBox: 86.399996 0.000000 3787.593569 2167.199897
%%BoundingBox: 43 43 1721 1037
%%HiResBoundingBox: 43.199998 43.199998 1720.799918 1036.799951
%%BoundingBox: -4 0 5764 3248
%%HiResBoundingBox: -3.571875 0.000000 5763.599725 3247.199845
%%BoundingBox: 43 129 1628 1037
%%HiResBoundingBox: 43.199998 129.599994 1627.199922 1036.799951
%%BoundingBox: 43 122 1642 1037
%%HiResBoundingBox: 43.199998 122.399994 1641.599922 1036.799951
%%BoundingBox: 43 49 1822 1037
%%HiResBoundingBox: 43.199998 49.584373 1821.599913 1036.799951
%%BoundingBox: 43 86 1678 1037
%%HiResBoundingBox: 43.199998 86.399996 1677.599920 1036.799951
%%BoundingBox: 43 43 1844 1037
%%HiResBoundingBox: 43.199998 43.199998 1843.199912 1036.799951
%%BoundingBox: 43 78 1858 1037
%%HiResBoundingBox: 43.199998 78.974996 1857.599911 1036.799951
%%BoundingBox: -4 0 5764 3248
%%HiResBoundingBox: -3.571875 0.000000 5763.599725 3247.199845
%%BoundingBox: 43 86 1707 1037
%%HiResBoundingBox: 43.199998 86.399996 1706.399919 1036.799951
%%BoundingBox: 43 259 1722 1037
%%HiResBoundingBox: 43.199998 259.199988 1721.981168 1036.799951
%%BoundingBox: 43 280 1743 1037
%%HiResBoundingBox: 43.199998 280.799987 1742.399917 1036.799951
%%BoundingBox: 43 103 1916 1037
%%HiResBoundingBox: 43.199998 103.978120 1915.199909 1036.799951
%%BoundingBox: 0 0 1923 1080
%%HiResBoundingBox: 0.000000 0.000000 1922.399908 1079.999949
%%BoundingBox: 0 0 1923 1080
%%HiResBoundingBox: 0.000000 0.000000 1922.399908 1079.999949
%%BoundingBox: 0 0 1923 1080
%%HiResBoundingBox: 0.000000 0.000000 1922.399908 1079.999949
%%BoundingBox: 43 40 1851 1037
%%HiResBoundingBox: 43.199998 40.978123 1850.990537 1036.799951
%%BoundingBox: 43 835 1138 1037
%%HiResBoundingBox: 43.199998 835.199960 1137.599946 1036.799951
%%BoundingBox: 43 172 1642 1037
%%HiResBoundingBox: 43.199998 172.799992 1641.599922 1036.799951
%%BoundingBox: 43 0 1872 1037
%%HiResBoundingBox: 43.199998 0.000000 1871.999911 1036.799951
%%BoundingBox: -4 0 5764 3248
%%HiResBoundingBox: -3.571875 0.000000 5763.599725 3247.199845
%%BoundingBox: 43 813 447 1037
%%HiResBoundingBox: 43.199998 813.599961 446.399979 1036.799951
%%BoundingBox: 43 39 1865 1037
%%HiResBoundingBox: 43.199998 39.993748 1864.799911 1036.799951
%%BoundingBox: 83 0 1837 1047
%%HiResBoundingBox: 83.981246 0.000000 1836.984287 1046.981200
%%BoundingBox: 0 0 1923 1080
%%HiResBoundingBox: 0.000000 0.000000 1922.399908 1079.999949
%%BoundingBox: 0 0 1800 1080
%%HiResBoundingBox: 0.000000 0.000000 1799.999914 1079.999949
%%BoundingBox: 0 0 1736 1080
%%HiResBoundingBox: 0.000000 0.000000 1735.199917 1079.999949
%%BoundingBox: 0 0 1923 1080
%%HiResBoundingBox: 0.000000 0.000000 1922.399908 1079.999949
%%BoundingBox: -4 0 5764 3248
%%HiResBoundingBox: -3.571875 0.000000 5763.599725 3247.199845
%%BoundingBox: 43 0 1923 1080
%%HiResBoundingBox: 43.199998 0.000000 1922.399908 1079.999949
%%BoundingBox: 43 57 1865 1037
%%HiResBoundingBox: 43.199998 57.599997 1864.799911 1036.799951
%%BoundingBox: 43 79 1753 1037
%%HiResBoundingBox: 43.199998 79.199996 1752.609291 1036.799951
%%BoundingBox: -4 0 5764 3248
%%HiResBoundingBox: -3.571875 0.000000 5763.599725 3247.199845
LukeL99 commented 6 years ago

Working in this branch.

https://github.com/rocketwagon/pdf-compressor/tree/feature/aspect-ratio-from-file

Bounding box is the bounds of the content, not the page. Need to find another way to get this information.