Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Jpeg Exif Orientation #1482

Merged
merged 10 commits into from
Dec 3, 2023
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- Add support for PDF/A-1b, PDF/A-1a, PDF/A-2b, PDF/A-2a, PDF/A-3b, PDF/A-3a
- Update crypto-js to v4.2.0 (properly fix security issue)

- Add support for EXIF orientation on JPEG images (#626 and #1353)

### [v0.13.0] - 2021-10-24

- Add tiling pattern support
Expand Down
29 changes: 15 additions & 14 deletions docs/images.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@ rendered at the current point in the text flow (below the last line of text).
Otherwise, it is positioned absolutely at the specified point. The image will
be scaled according to the following options.

* Neither `width` or `height` provided - image is rendered at full size
* `width` provided but not `height` - image is scaled proportionally to fit in the provided `width`
* `height` provided but not `width` - image is scaled proportionally to fit in the provided `height`
* Both `width` and `height` provided - image is stretched to the dimensions provided
* `scale` factor provided - image is scaled proportionally by the provided scale factor
* `fit` array provided - image is scaled proportionally to fit within the passed width and height
* `cover` array provided - image is scaled proportionally to completely cover the rectangle defined by the passed width and height
* `link` - a URL to link this image to (shortcut to create an annotation)
* `goTo` - go to anchor (shortcut to create an annotation)
* `destination` - create anchor to this image
- Neither `width` or `height` provided - image is rendered at full size
- `width` provided but not `height` - image is scaled proportionally to fit in the provided `width`
- `height` provided but not `width` - image is scaled proportionally to fit in the provided `height`
- Both `width` and `height` provided - image is stretched to the dimensions provided
- `scale` factor provided - image is scaled proportionally by the provided scale factor
- `fit` array provided - image is scaled proportionally to fit within the passed width and height
- `cover` array provided - image is scaled proportionally to completely cover the rectangle defined by the passed width and height
- `link` - a URL to link this image to (shortcut to create an annotation)
- `goTo` - go to anchor (shortcut to create an annotation)
- `destination` - create anchor to this image
- `ignoreOrientation` - (true/false) ignore JPEG EXIF orientation. By default, images with JPEG EXIF orientation are properly rotated and/or flipped. Defaults to `false`, unless `ignoreOrientation` option set to `true` when creating the `PDFDocument` object (e.g. `new PDFDocument({ignoreOrientation: true})`)

When a `fit` or `cover` array is provided, PDFKit accepts these additional options:

* `align` - horizontally align the image, the possible values are `'left'`, `'center'` and `'right'`
* `valign` - vertically align the image, the possible values are `'top'`, `'center'` and `'bottom'`
- `align` - horizontally align the image, the possible values are `'left'`, `'center'` and `'right'`
- `valign` - vertically align the image, the possible values are `'top'`, `'center'` and `'bottom'`

Here is an example showing some of these options.

Expand All @@ -48,11 +49,11 @@ Here is an example showing some of these options.
.rect(430, 15, 100, 100).stroke()
.text('Centered', 430, 0);

* * *
---

This example produces the following output:

![0](images/images.png "400")
![0](images/images.png '400')

That is all there is to adding images to your PDF documents with PDFKit. Now
let's look at adding outlines.
5 changes: 5 additions & 0 deletions lib/image/jpeg.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import exif from 'jpeg-exif';

const MARKERS = [
0xffc0,
0xffc1,
Expand Down Expand Up @@ -31,6 +33,9 @@ class JPEG {
throw 'SOI not found in JPEG';
}

// Parse the EXIF orientation
this.orientation = exif.fromBuffer(this.data).Orientation || 1;

let pos = 2;
while (pos < this.data.length) {
marker = this.data.readUInt16BE(pos);
Expand Down
132 changes: 118 additions & 14 deletions lib/mixins/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ export default {
},

image(src, x, y, options = {}) {
let bh, bp, bw, image, ip, left, left1;
let bh, bp, bw, image, ip, left, left1, rotateAngle, originX, originY;
if (typeof x === 'object') {
options = x;
x = null;
}

// Ignore orientation based on document options or image options
const ignoreOrientation =
options.ignoreOrientation ||
(options.ignoreOrientation !== false && this.options.ignoreOrientation);

x = (left = x != null ? x : options.x) != null ? left : this.x;
y = (left1 = y != null ? y : options.y) != null ? left1 : this.y;

Expand All @@ -36,24 +41,31 @@ export default {
this.page.xobjects[image.label] = image.obj;
}

let w = options.width || image.width;
let h = options.height || image.height;
let { width, height } = image;

// If EXIF orientation calls for it, swap width and height
if (!ignoreOrientation && image.orientation > 4) {
[width, height] = [height, width];
}

let w = options.width || width;
let h = options.height || height;

if (options.width && !options.height) {
const wp = w / image.width;
w = image.width * wp;
h = image.height * wp;
const wp = w / width;
w = width * wp;
h = height * wp;
} else if (options.height && !options.width) {
const hp = h / image.height;
w = image.width * hp;
h = image.height * hp;
const hp = h / height;
w = width * hp;
h = height * hp;
} else if (options.scale) {
w = image.width * options.scale;
h = image.height * options.scale;
w = width * options.scale;
h = height * options.scale;
} else if (options.fit) {
[bw, bh] = options.fit;
bp = bw / bh;
ip = image.width / image.height;
ip = width / height;
if (ip > bp) {
w = bw;
h = bw / ip;
Expand All @@ -64,7 +76,7 @@ export default {
} else if (options.cover) {
[bw, bh] = options.cover;
bp = bw / bh;
ip = image.width / image.height;
ip = width / height;
if (ip > bp) {
h = bh;
w = bh * ip;
Expand All @@ -88,6 +100,91 @@ export default {
}
}

if (!ignoreOrientation) {
switch (image.orientation) {
// No orientation (need to flip image, though, because of the default transform matrix on the document)
default:
case 1:
h = -h;
y -= h;

rotateAngle = 0;
break;
// Flip Horizontal
case 2:
w = -w;
h = -h;
x -= w;
y -= h;

rotateAngle = 0;
break;
// Rotate 180 degrees
case 3:
originX = x;
originY = y;

h = -h;
x -= w;

rotateAngle = 180;
break;
// Flip vertical
case 4:
// Do nothing, image will be flipped

break;
// Flip horizontally and rotate 270 degrees CW
case 5:
originX = x;
originY = y;

[w, h] = [h, w];
y -= h;

rotateAngle = 90;
break;
// Rotate 90 degrees CW
case 6:
originX = x;
originY = y;

[w, h] = [h, w];
h = -h;

rotateAngle = 90;
break;
// Flip horizontally and rotate 90 degrees CW
case 7:
originX = x;
originY = y;

[w, h] = [h, w];
h = -h;
w = -w;
x -= w;

rotateAngle = 90;
break;
// Rotate 270 degrees CW
case 8:
originX = x;
originY = y;

[w, h] = [h, w];
h = -h;
x -= w;
y -= h;

rotateAngle = -90;
break;
}
} else {
h = -h;
y -= h;
rotateAngle = 0;
}

// create link annotations if the link option is given
if (options.link != null) {
this.link(x, y, w, h, options.link);
Expand All @@ -105,7 +202,14 @@ export default {
}

this.save();
this.transform(w, 0, 0, -h, x, y + h);

if (rotateAngle) {
this.rotate(rotateAngle, {
origin: [originX, originY]
});
}

this.transform(w, 0, 0, h, x, y);
this.addContent(`/${image.label} Do`);
this.restore();

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"dependencies": {
"crypto-js": "^4.2.0",
"fontkit": "^1.8.1",
"jpeg-exif": "^1.1.4",
"linebreak": "^1.0.2",
"png-js": "^1.0.0"
},
Expand Down
3 changes: 2 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const external = [
'linebreak',
'png-js',
'crypto-js',
'saslprep'
'saslprep',
'jpeg-exif',
];

export default [
Expand Down
Binary file added tests/images/orientation-1.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/images/orientation-2.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/images/orientation-3.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/images/orientation-4.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/images/orientation-5.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/images/orientation-6.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/images/orientation-7.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/images/orientation-8.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.