cantoo-scribe / pdf-lib

Create and modify PDF documents in any JavaScript environment
https://pdf-lib.js.org
MIT License
136 stars 27 forks source link

drawRectangle with rotation produces an unrotated parallelogram #61

Closed CDSeal closed 2 months ago

CDSeal commented 2 months ago

What were you trying to do?

Draw a rotated rectangle

How did you attempt to do it?

I used

PDFPage.drawRectangle({x: 20, y: 200, width: 100, height: 50, rotate: degrees(45)})

What actually happened?

An unrotated parallelogram appeared on the page, far more to the right than 20pt and slanted to the right by 45 degrees.

What did you expect to happen?

I expected a rectangle to appear at 20, 200, rotated by 45 degrees.

How can we reproduce the issue?

const fs = require('fs');
const pdf = require('@cantoo/pdf-lib');
run();
async function run() {
    const doc = await pdf.PDFDocument.create();
    const page = doc.addPage();
    page.drawRectangle({x: 20, y: 200, width: 100, height: 50, rotate: pdf.degrees(45)});
    fs.writeFileSync('bugreport.pdf', await doc.save());
}

Version

2.2.1

What environment are you running pdf-lib in?

Node

Checklist

Additional Notes

Hopding's original implementation works as expected.

The manipulation of the transformation matrix applied in https://github.com/cantoo-scribe/pdf-lib/blob/1b6d94aa778a7cfa40decc0cb7f2ed14947cfd43/src/api/operations.ts#L269 won't lead to a rotation.

Sharcoux commented 2 months ago

Can you test version 2.2.3-0 which should solve your problem? Please report so that I can release this pre-patch.

CDSeal commented 2 months ago

Thanks for the quick response!

It won't run in my bugreport project (with the above code), though, where I installed it first. I deleted node_modules/ and package-lock.json and ran npm install @cantoo/pdf-lib@2.2.3-0 and then also installed tslib.

Here's what I get when I run it:

user@host bugreport % node bugreport.js
node:internal/modules/cjs/loader:1251
  throw err;
  ^

Error: Cannot find module 'src/types/matrix'
Require stack:
- /Users/user/git/bugreport/node_modules/@cantoo/pdf-lib/cjs/api/operations.js
- /Users/user/git/bugreport/node_modules/@cantoo/pdf-lib/cjs/api/form/appearances.js
- /Users/user/git/bugreport/node_modules/@cantoo/pdf-lib/cjs/api/form/index.js
- /Users/user/git/bugreport/node_modules/@cantoo/pdf-lib/cjs/api/index.js
- /Users/user/git/bugreport/node_modules/@cantoo/pdf-lib/cjs/index.js
- /Users/user/git/bugreport/bugreport.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1248:15)
    at Module._load (node:internal/modules/cjs/loader:1074:27)
    at TracingChannel.traceSync (node:diagnostics_channel:315:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:217:24)
    at Module.require (node:internal/modules/cjs/loader:1339:12)
    at require (node:internal/modules/helpers:125:16)
    at Object.<anonymous> (/Users/user/git/bugreport/node_modules/@cantoo/pdf-lib/cjs/api/operations.js:10:18)
    at Module._compile (node:internal/modules/cjs/loader:1546:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1691:10)
    at Module.load (node:internal/modules/cjs/loader:1317:32) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/user/git/bugreport/node_modules/@cantoo/pdf-lib/cjs/api/operations.js',
    '/Users/user/git/bugreport/node_modules/@cantoo/pdf-lib/cjs/api/form/appearances.js',
    '/Users/user/git/bugreport/node_modules/@cantoo/pdf-lib/cjs/api/form/index.js',
    '/Users/user/git/bugreport/node_modules/@cantoo/pdf-lib/cjs/api/index.js',
    '/Users/user/git/bugreport/node_modules/@cantoo/pdf-lib/cjs/index.js',
    '/Users/user/git/bugreport/bugreport.js'
  ]
}

Node.js v22.6.0

I don't know how that ts stuff works, but shouldn't it require the cjs version? A file named matrix.js exists in cjs/types/, but not in src/types/, ofc.

Please note that I'll be unavailable starting on Wednesday and won't be able to test from then until mid-September.

CDSeal commented 2 months ago

I manually changed the corresponding require in node_modules/@cantoo/pdf-lib/cjs/api/operations.js to const matrix_1 = require("../types/matrix");

It now runs and does a rotation, but the rectangle is still too far to the right and not high enough up, and it is rotated by -45° instead of 45° as expected.

Expected result (created with original pdf-lib): expected.pdf

Actual result with v.2.2.3-0: v2.2.3-0.pdf

Sharcoux commented 2 months ago

Solved in version 2.2.3

CDSeal commented 2 months ago

Works, thanks!