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

User friendly page rotation #17

Open
jamie-lemon opened this issue Feb 20, 2024 · 5 comments
Open

User friendly page rotation #17

jamie-lemon opened this issue Feb 20, 2024 · 5 comments

Comments

@jamie-lemon
Copy link
Collaborator

Can we do something in our mupdf.js file to allow for an easy way to rotate a page?

In PyMUPDF we have: https://pymupdf.readthedocs.io/en/latest/the-basics.html#rotating-a-pdf , but I can't see the equivalent in our API.

Please let's add this to the API to match the way PyMuPDF provides it.

@ccxvii
Copy link
Collaborator

ccxvii commented Feb 21, 2024

Here's an example of how to rotate a page with our existing API:

const fs = require("fs")
const mupdf = require("mupdf")

var doc = mupdf.Document.openDocument(fs.readFileSync("input.pdf"), "application/pdf")
var page = doc.loadPage(0)

// Get the PDF object corresponding to the page
var page_obj = page.getObject()

// Look up the existing page rotation (inheritable from the page tree, see PDF reference for details)
var rotate = page_obj.getInheritable("Rotate")
if (!rotate)
    rotate = 0

// Update the Rotate value
page_obj.put("Rotate", rotate + 90)

// Save the changes to a new file.
fs.writeFileSync("output.pdf", doc.saveToBuffer("incremental").asUint8Array())

@ccxvii ccxvii closed this as completed Feb 21, 2024
@jamie-lemon
Copy link
Collaborator Author

I get this - why can't we bundle the 4 lines which do the work into mupdf.js as a method? So I just do e.g. page.rotate(90)

@jamie-lemon jamie-lemon reopened this Feb 21, 2024
@jamie-lemon
Copy link
Collaborator Author

@ccxvii When I try your code sample I get: TypeError: page.getObject is not a function

@jamie-lemon
Copy link
Collaborator Author

Oh wait - I'm just appreciating the other recent commits - I'll have to package up a local mupdf node package

@jamie-lemon
Copy link
Collaborator Author

@ccxvii Opening this again as in the sample code I think the rotate variable is a pointer and not a rotation value as such.
i.e.

// Look up the existing page rotation (inheritable from the page tree, see PDF reference for details)
    let rotate = page_obj.getInheritable("Rotate")
    console.log("rotate="+rotate) // rotate=11845304
    if (!rotate)
        rotate = 0

    // Update the Rotate value
    page_obj.put("Rotate", rotate + 90)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants