0
  [demo][1]

    [1]: https://codesandbox.io/s/react-to-print-rzdhd

I'm using react-to-print library to generate pdf from html. But the problem is that when I try to generate pdf in my mobile(small screen size), it does not work for me however, it works in desktop(large screen size) view.

1 Answer 1

0

This package uses window.print(), which won't work on mobile devices. You can use the following workaround:

  class Example extends React.Component {
    printPDF(e) {
      return new Promise((resolve: any) => {
        e.preventDefault()
        var ua = navigator.userAgent.toLowerCase()
        var isAndroid = ua.indexOf('android') > -1 //&& ua.indexOf("mobile");
        if (isAndroid) {
          // https://developers.google.com/cloud-print/docs/gadget
          var gadget = new cloudprint.Gadget()

          gadget.openPrintDialog()
        } else {
          window.print()
        }
        resolve(true)
      })
    }

    render() {
      return (
        <div>
          <ReactToPrint
            trigger={() => <button>Print this out!</button>}
            print={e => this.printPDF(e)} // HANDLE PRINT FUNCTION
            content={() => this.componentRef}
          />
          <ComponentToPrint ref={el => (this.componentRef = el)} />
        </div>
      )
    }
  }
Sign up to request clarification or add additional context in comments.

7 Comments

It gives me the following error: Cannot find name 'cloudprint'
Please check this Documentation
Also you can use this package
Google Cloud Print will no longer be supported as of December 31, 2020. Please see the support article for help migrating.
Not able to install package cloud-print
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.