Skip to content

fix: scale each imported pdf page independently to fit the document format - #1835

Open
alberto-is wants to merge 2 commits into
flxzt:mainfrom
alberto-is:fix/pdf-import-mixed-page-formats
Open

alberto-is wants to merge 2 commits into
flxzt:mainfrom
alberto-is:fix/pdf-import-mixed-page-formats

Conversation

@alberto-is

Copy link
Copy Markdown
Contributor

Fixes #1834

When importing a PDF containing pages with different formats, such as a mix of portrait and landscape pages, the pages could become misaligned when Adjust document was enabled.

This happened because all pages were scaled using the dimensions of the first page. Pages with a different format therefore did not fit correctly in the document layout.

Changes

  • Scale each imported PDF page independently when Adjust document is enabled.
  • Keep each page's aspect ratio while fitting it within the document format.
  • Preserve the existing import behavior when Adjust document is disabled.
  • Apply the fix to both bitmap and vector PDF imports.

Testing

  • cargo fmt --all -- --check
  • cargo clippy --workspace --all-targets -- -D warnings
  • cargo test --workspace

The following video shows the result before and after the fix when importing a PDF with mixed page formats:

format.import.pdf.mp4

@alberto-is
alberto-is marked this pull request as draft August 26, 2026 15:16
@alberto-is

Copy link
Copy Markdown
Contributor Author

This is a design decision I'd like to flag before merging.

The min() in page_zoom_fit gives the page a "contain" behavior: each page is scaled to fit entirely within the document page format while preserving its aspect ratio. The trade-off is that when a page's aspect ratio doesn't match the format, there can be empty space on one side. For example, a portrait page imported into a landscape-oriented format may leave blank space to the right.

An alternative would be to scale by width only (page_width / intrinsic_width), which would always fill the full width. The height would then match the page's natural size, and since the document is already expanded to fit the content via set_height, nothing would be clipped.

I kept the min() approach because it ensures the page is never distorted or cropped. However, if it's preferred to always fill the full width, it's a one-line change and I'm happy to update it.

Any preference on which behavior we should go with?

@alberto-is
alberto-is marked this pull request as ready for review August 26, 2026 15:35
@Doublonmousse

Copy link
Copy Markdown
Collaborator

The min() in page_zoom_fit gives the page a "contain" behavior: each page is scaled to fit entirely within the document page format while preserving its aspect ratio. The trade-off is that when a page's aspect ratio doesn't match the format, there can be empty space on one side. For example, a portrait page imported into a landscape-oriented format may leave blank space to the right.

This way you guarantee that a pdf page is always inside a rnote page on export. Wouldn't you cut your page (portrait page into a landscape pdf document) into two with the second option?

Your video shows the second option if I'm not mistaken

@alberto-is

Copy link
Copy Markdown
Contributor Author

Hi, the video shows the first option, but in this case the format matches ( I found the problem because I tried it later with other PDFs). The thing is, the main problem is divided into two: the min to contain sometimes leaves the white space, and the scale only takes the first page into consideration. I corrected the latter because the former is more like a "feature result", since the pages are only cut when we merge formats (but that was usual too before this PR):

image

If we want to avoid cutting the pages, we would need to check whether each page fits in the remaining space of the current layout, taking into consideration the pages that have already been imported. The downside is that this could leave some white space between pages. For example, in this case, with the first option, the page gets cut:

image image

We have a horizontal page that only occupies half of the layout, so the next page goes into the other half. Since it doesn't fit in the remaining space, it gets cut. So, maybe having some white space between pages would be better?

As I said, this happens because we calculate whether the page fits in the format, but we don't take into account that the format has already been partially occupied. The thing is that each case is more like a "personal preference", right? For me, for example, I prefer the pages to be cut rather than having white spaces between them, and I don't really mind a "small" gap on the right. But I can implement the second solution, or even add the calculations to leave white space when needed to avoid cutting the pages (I don't think both at the same time are possible because, in any case, one page could occupy several pages, with one page and a bit being the most common case) if it is more aligned with your vision.

@flxzt

flxzt commented Sep 8, 2026

Copy link
Copy Markdown
Owner

I wonder if it is even desirable to always fit the document pages uniformly into the Rnote document width.

If a PDF has multiple size pages (e.g. A4 and A3), should that not be reflected in the Rnote document as well?

Another idea for a solution would be to take the maximum width of all PDF pages and scale them to this value accordingly.

@alberto-is

Copy link
Copy Markdown
Contributor Author

I can update the PR to use a single zoom level for all pages, based on the maximum intrinsic width, so that the widest PDF page fits page_width while the others keep their relative size. I assume that in this case, we don't need to consider it a problem if a very tall page exceeds format.height() and gets split on export, since that was already the previous behavior.

Btw, with this approach, narrower pages will leave empty space on the right if left-aligned (as is the current behavior). Do you prefer to keep them left-aligned, or center them?

@flxzt

flxzt commented Sep 11, 2026

Copy link
Copy Markdown
Owner

Good question, I'll leave it up to you.
Btw. is this approach what makes sense to you as well or would you prefer your initial version?

@alberto-is
alberto-is force-pushed the fix/pdf-import-mixed-page-formats branch from 629f525 to c4d787d Compare September 11, 2026 21:35
@alberto-is

Copy link
Copy Markdown
Contributor Author

So, I went with the max-width approach; it’s much better than my initial idea. I updated the PR to use a single zoom level based on the widest page and dropped the per-page fit from the initial version.

Since I personally prefer keeping all the pages together because I don’t use the export that much, I also enabled “Page Spacing” with “Adjust Document”, so “One per Document Page” can avoid splitting pages on export for those who use it. I also centered the narrower pages, but I can left-align them instead if you prefer.

Btw (x3), I applied the zoom changes to both modes (Adjust Document on/off) because it didn’t make much sense to me to keep the zoom level based on the first page.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Importing pdf with different page formats breaks "adjust document" option

3 participants