Skip to content

fix(PdfReader): render pdf when set ShowToolbar is false#823

Merged
ArgoZhang merged 2 commits intomasterfrom
feat-pdf
Dec 14, 2025
Merged

fix(PdfReader): render pdf when set ShowToolbar is false#823
ArgoZhang merged 2 commits intomasterfrom
feat-pdf

Conversation

@ArgoZhang
Copy link
Copy Markdown
Member

@ArgoZhang ArgoZhang commented Dec 14, 2025

Link issues

fixes #822

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Bug Fixes:

  • Prevent null reference errors in PdfReader JavaScript when the document title, scale control, page number input, or two-page view control elements are missing.

Copilot AI review requested due to automatic review settings December 14, 2025 05:49
@bb-auto bb-auto Bot added the bug Something isn't working label Dec 14, 2025
@bb-auto bb-auto Bot added this to the v9.2.0 milestone Dec 14, 2025
@ArgoZhang ArgoZhang merged commit 5008fbb into master Dec 14, 2025
3 checks passed
@ArgoZhang ArgoZhang deleted the feat-pdf branch December 14, 2025 05:49
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Dec 14, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Updates the PdfReader JavaScript to safely interact with optional toolbar and metadata elements so that PDFs still render correctly when the toolbar or title elements are absent.

Flow diagram for loadMetadata handling optional docTitle element

flowchart TD
    A[loadMetadata called with el, pdfViewer, metadata] --> B[Find filename element .bb-view-pdf-dialog-filename]
    B --> C[Find docTitle element .bb-view-subject]
    C --> D{docTitle exists?}
    D -->|Yes| E[Set filename.textContent to docTitle.textContent]
    D -->|No| F[Skip setting filename text]
    E --> G[Find filesize element .bb-view-pdf-dialog-filesize]
    F --> G[Find filesize element .bb-view-pdf-dialog-filesize]
    G --> H["Set filesize.textContent to getFilesize(metadata)"]
Loading

Flow diagram for resetToolbarView with optional toolbar elements

flowchart TD
    A[resetToolbarView called with el, pdfViewer] --> B[Find scaleEl .bb-view-scale-input]
    B --> C{scaleEl exists?}
    C -->|Yes| D[updateScaleValue with pdfViewer.currentScale]
    C -->|No| E[Skip scale update]
    D --> F[Find pageEl .bb-view-num]
    E --> F[Find pageEl .bb-view-num]
    F --> G{pageEl exists?}
    G -->|Yes| H[Set pageEl.value to pdfViewer.currentPageNumber]
    G -->|No| I[Skip page value update]
    H --> J[Find group .bb-view-group-rotate]
    I --> J[Find group .bb-view-group-rotate]
    J --> K{group exists?}
    K -->|Yes| L[Remove rotate-left and rotate-right classes]
    K -->|No| M[Skip rotate reset]
    L --> N[Find twoPagesOneView .dropdown-item-pages]
    M --> N[Find twoPagesOneView .dropdown-item-pages]
    N --> O{twoPagesOneView exists?}
    O -->|Yes| P{pdfViewer.spreadMode === 1?}
    O -->|No| S[Skip spread mode UI update]
    P -->|Yes| Q[Add active class]
    P -->|No| R[Remove active class]
    Q --> T[Delete el.widths]
    R --> T[Delete el.widths]
    S --> T[Delete el.widths]
Loading

File-Level Changes

Change Details Files
Guard DOM access for optional metadata title element when loading PDF metadata.
  • Wrap access to the subject/title element in a null check before assigning its text to the filename element
  • Preserve existing behavior of displaying the document title as the filename when the subject element exists
src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js
Guard toolbar-related DOM updates so PdfReader works when the toolbar is hidden or partially absent.
  • Check for existence of the scale input element before calling the function that updates the zoom value
  • Check for existence of the page number input element before assigning the current page number
  • Check for existence of the two-pages-per-view dropdown item before toggling its active state based on the viewer spread mode
src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js

Assessment against linked issues

Issue Objective Addressed Explanation
#822 Ensure the PdfReader component correctly renders PDFs when ShowToolbar is set to false (i.e., when toolbar DOM elements are absent).
#822 Prevent JavaScript errors in PdfReader logic when toolbar-related DOM elements (filename, scale input, page number, spread mode toggle) are missing.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes - here's some feedback:

  • Now that several toolbar-related elements are optional, consider centralizing the null-check logic (e.g., a small helper like setTextIfExists(selector, value)) to avoid repetitive querySelector + if patterns and keep future changes less error-prone.
  • In resetToolbarView, scaleEl and pageEl are queried but only used to gate calls to updateScaleValue / pageEl.value; if those functions can safely handle missing elements, you could pass the element into them or early-return when the toolbar is disabled to simplify the branching.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Now that several toolbar-related elements are optional, consider centralizing the null-check logic (e.g., a small helper like `setTextIfExists(selector, value)`) to avoid repetitive `querySelector` + `if` patterns and keep future changes less error-prone.
- In `resetToolbarView`, `scaleEl` and `pageEl` are queried but only used to gate calls to `updateScaleValue` / `pageEl.value`; if those functions can safely handle missing elements, you could pass the element into them or early-return when the toolbar is disabled to simplify the branching.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug where the PdfReader component fails to render PDFs when the ShowToolbar parameter is set to false. The fix adds null checks for DOM elements that may not exist when the toolbar is hidden, preventing JavaScript runtime errors.

  • Adds defensive null checks for toolbar-related DOM elements in resetToolbarView function
  • Adds a null check for the docTitle element in loadMetadata function
  • Bumps the package version from 10.0.15 to 10.0.16

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js Adds null checks for toolbar elements (scaleEl, pageEl, twoPagesOneView, docTitle) that may not exist when ShowToolbar is false
src/components/BootstrapBlazor.PdfReader/BootstrapBlazor.PdfReader.csproj Version bump from 10.0.15 to 10.0.16 for the bug fix release
Comments suppressed due to low confidence (9)

src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js:233

  • Multiple DOM elements in loadMetadata are accessed without null checks: filesize (line 205), created (line 212), application (line 217), producer (line 220), version (line 223), count (line 226), and size (line 229). These elements are part of a PDF info dialog that may not be rendered when ShowToolbar is false. Without null checks, accessing .textContent on these null elements will cause runtime errors. Add null checks for all these elements similar to the pattern used for docTitle.
    const filesize = el.querySelector('.bb-view-pdf-dialog-filesize');
    filesize.textContent = getFilesize(metadata);

    const title = el.querySelector('.bb-view-pdf-dialog-title');
    const author = el.querySelector('.bb-view-pdf-dialog-author');
    const subject = el.querySelector('.bb-view-pdf-dialog-subject');
    const keywords = el.querySelector('.bb-view-pdf-dialog-keywords');
    const created = el.querySelector('.bb-view-pdf-dialog-created');
    created.textContent = parsePdfDate(metadata.info.CreationDate)?.toLocaleString();

    const modified = el.querySelector('.bb-view-pdf-dialog-modified');

    const application = el.querySelector('.bb-view-pdf-dialog-application');
    application.textContent = metadata.info.Creator;

    const producer = el.querySelector('.bb-view-pdf-dialog-producer');
    producer.textContent = metadata.info.Producer;

    const version = el.querySelector('.bb-view-pdf-dialog-version');
    version.textContent = metadata.info.PDFFormatVersion;

    const count = el.querySelector('.bb-view-pdf-dialog-count');
    count.textContent = pdfViewer.pagesCount;

    const size = el.querySelector('.bb-view-pdf-dialog-size');
    pdfViewer.pdfDocument.getPage(pdfViewer.currentPageNumber).then(page => {
        const viewport = page.getViewport({ scale: 1 });
        size.textContent = `${(viewport.width / 72).toFixed(2)} * ${(viewport.height / 72).toFixed(2)} in (portrait)`;
    });

src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js:208

  • Unused variable title.
    const title = el.querySelector('.bb-view-pdf-dialog-title');

src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js:209

  • Unused variable author.
    const author = el.querySelector('.bb-view-pdf-dialog-author');

src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js:210

  • Unused variable subject.
    const subject = el.querySelector('.bb-view-pdf-dialog-subject');

src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js:211

  • Unused variable keywords.
    const keywords = el.querySelector('.bb-view-pdf-dialog-keywords');

src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js:215

  • Unused variable modified.
    const modified = el.querySelector('.bb-view-pdf-dialog-modified');

src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js:235

  • Unused variable webview.
    const webview = el.querySelector('.bb-view-pdf-dialog-webview');

src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js:705

  • The initial value of v is unused, since it is always overwritten.
    let v = 100;

src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js:518

  • Avoid automated semicolon insertion (95% of all statements in the enclosing function have an explicit semicolon).
    })

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

const filename = el.querySelector('.bb-view-pdf-dialog-filename');
const docTitle = el.querySelector('.bb-view-subject');
filename.textContent = docTitle.textContent;
if (docTitle) {
Copy link

Copilot AI Dec 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The null check for docTitle is added, but filename (line 199) is still accessed without a null check. If docTitle is null when ShowToolbar is false, then filename is also likely to be null. This will cause a runtime error when trying to set filename.textContent if docTitle is null. Add a null check for filename as well, or wrap both checks together.

Suggested change
if (docTitle) {
if (filename && docTitle) {

Copilot uses AI. Check for mistakes.
}
}

delete el.widths
Copy link

Copilot AI Dec 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid automated semicolon insertion (91% of all statements in the enclosing function have an explicit semicolon).

Suggested change
delete el.widths
delete el.widths;

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(PdfReader): render pdf when set ShowToolbar is false

2 participants