Skip to content

Commit

Permalink
retains DPI from input images (thanks to Steven Lee)
Browse files Browse the repository at this point in the history
  • Loading branch information
zdenop committed Jul 3, 2012
1 parent fdf40be commit f24661a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
4 changes: 4 additions & 0 deletions AUTHORS
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Author:
=======
Adam Langley <[email protected]>

Contributors:
=============
Misty De Meo <[email protected]>
zdenop <[email protected]>
Steven Lee http://www.rubypdf.com
3 changes: 2 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
0.28: (not yet released)
* Update to the latest Leptonica (1.68)
* pdf.py now correctly retains DPI from input images (thanks to RubyPDF)
* autotools support, VC++ 2008 solution
* fix binary file open mode on Windows
* version info (-V --version)
* pdf.py now correctly retains DPI from input images (thanks to Steven Lee
http://blog.rubypdf.com/2011/09/09/jbig2-pdf-py-patch-the-right-way-to-get-dpi/)

0.27:
* Update to the latest Leptonica (1.58)
Expand Down
31 changes: 27 additions & 4 deletions pdf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
#!/usr/bin/python
# Copyright 2006 Google Inc.
# Author: [email protected] (Adam Langley)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# JBIG2 Encoder
# https://github.com/agl/jbig2enc

import sys
import re
Expand All @@ -11,7 +28,7 @@
# Run ./jbig2 -s -p <other options> image1.jpeg image1.jpeg ...
# python pdf.py output > out.pdf

dpi = 600
dpi = 72

class Ref:
def __init__(self, x):
Expand Down Expand Up @@ -121,16 +138,22 @@ def main(symboltable='symboltable', pagefiles=glob.glob('page-*')):
except IOError:
sys.stderr.write("error reading page file %s\n"% p)
continue
(width, height) = struct.unpack('>II', contents[11:19])
(width, height, xres, yres) = struct.unpack('>IIII', contents[11:27])

if xres == 0:
xres = dpi
if yres == 0:
yres = dpi

xobj = Obj({'Type': '/XObject', 'Subtype': '/Image', 'Width':
str(width), 'Height': str(height), 'ColorSpace': '/DeviceGray',
'BitsPerComponent': '1', 'Filter': '/JBIG2Decode', 'DecodeParms':
' << /JBIG2Globals %d 0 R >>' % symd.id}, contents)
contents = Obj({}, 'q %f 0 0 %f 0 0 cm /Im1 Do Q' % (float(width * 72) / dpi, float(height * 72) / dpi))
contents = Obj({}, 'q %f 0 0 %f 0 0 cm /Im1 Do Q' % (float(width * 72) / xres, float(height * 72) / yres))
resources = Obj({'ProcSet': '[/PDF /ImageB]',
'XObject': '<< /Im1 %d 0 R >>' % xobj.id})
page = Obj({'Type': '/Page', 'Parent': '3 0 R',
'MediaBox': '[ 0 0 %f %f ]' % (float(width * 72) / dpi, float(height * 72) / dpi),
'MediaBox': '[ 0 0 %f %f ]' % (float(width * 72) / xres, float(height * 72) / yres),
'Contents': ref(contents.id),
'Resources': ref(resources.id)})
[doc.add_object(x) for x in [xobj, contents, resources, page]]
Expand Down

0 comments on commit f24661a

Please sign in to comment.