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

Mobile Chrome prints entire page instead of target content #526

Closed
stacksagar opened this issue Sep 11, 2022 · 10 comments
Closed

Mobile Chrome prints entire page instead of target content #526

stacksagar opened this issue Sep 11, 2022 · 10 comments

Comments

@stacksagar
Copy link

I'm trying to print PDF on mobile, It print entire page not only my specific component!!

@MatthewHerbst
Copy link
Owner

Hello. Could you be more specific please? What operating system and what browser? Also, if you could share your code that would be very helpful.

@xihelmy
Copy link

xihelmy commented Sep 12, 2022

I've same issue here, i'm using android & chrome for the browser

@MatthewHerbst
Copy link
Owner

@xihelmy any chance you could share some code please? A codesandbox replicating the issue would be ideal

@xihelmy
Copy link

xihelmy commented Sep 13, 2022

Layout print:

export const NPPKBDocument = forwardRef((props, ref) => {
  return (
    <div ref={ref}>
         // some view
    </div>
  );
});

React to print button :

export const ModalPrint = () => {
  const componentRef = useRef(null);

  const reactToPrintContent = useCallback(() => {
    return componentRef.current;
  }, [componentRef.current]);

  const reactToPrintTrigger = useCallback(() => {
    return (
      <button className="base-btn-primary fs-14 mr-3 float-right">
        Print
      </button>
    );
  }, []);

  const pageStyle = `
    @page {
      margin-top: 12mm;
      size: 120mm 140mm;
    }
  `;

  return (
    <div className="modal">
      <ReactToPrint
        content={reactToPrintContent}
        pageStyle={pageStyle}
        documentTitle="Dokumen"
        removeAfterPrint
        trigger={reactToPrintTrigger}
      />
    </div>
  );
};

It's look good on my PC, but when i try to print on mobile, it print entire page

@headstar
Copy link

headstar commented Sep 20, 2022

Same issue for me. Looks ok on Windows laptop + Chrome but prints entire page on iOS 15.6.1 + Chrome. Works as expected on iOS 15.6.1 + Safari.

@MatthewHerbst
Copy link
Owner

MatthewHerbst commented Sep 20, 2022

@headstar thanks for the datapoint! I'm fearing that this is an upstream issue with Chrome and not an issue with react-to-print, but haven't been able to pin down a specific change yet. Under the hood iOS Chrome still uses Gecko (the Safari JS engine) so it working on mobile Safari strongly points to an issue specific with mobile Chrome, especially since OP was using Android mobile Chrome

@MatthewHerbst
Copy link
Owner

I've also verified that the official demo CodeSandbox has the same issue when I try to print it on my Android Chrome

@MatthewHerbst MatthewHerbst changed the title Print PDF not working on mobile! Mobile Chrome prints entire page instead of target content Sep 20, 2022
@xihelmy
Copy link

xihelmy commented Sep 20, 2022

Sorry for the late response, I've solved this issue, by set removeAfterPrint={false}

@MatthewHerbst
Copy link
Owner

Oh, that's really interesting @xihelmy! It would mean then that mobile Chrome does not block when window.print is called, which would mean that the iframe we generate for printing would get removed before the print preview opens, and then when the browser looks for the root document it goes the the main page root. I'm honestly surprised it doesn't crash.

@MatthewHerbst
Copy link
Owner

Note: this is also being tracked in #187

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

4 participants