question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: PDFViewer is a web specific API. Using Electron.

See original GitHub issue

Description of bug Hello,

I am trying to make an Electron app which allows the user to modify the PDF, displays the changes immediately and allows the user to download or print the finished PDF document. All of this I want to be accessible in a modal React window.

At first, I tried to use the ReactPDF.render() method to save the PDF file locally and then display it but this causes my whole application to refresh. Is there any way to prevent this?

Then, I tried to use the PDFViewer component. However, this gives me the error:

Uncaught Error: PDFViewer is a web specific API. Or you're either using this component on Node, or your bundler is not loading react-pdf from the appropiate web build.

I searched for a solution to this error and found this https://github.com/diegomura/react-pdf/issues/908

I tried the solution which got rid of the error message, but now it’s not displaying anything. What am I doing wrong? Do I have to do something differently because I am using Electron?

To Reproduce Here is a simplified code snippet:

import React, { Component } from 'react';
import Modal from 'react-modal';
import {
    Page,
    Text,
    View,
    Document,
    Image,
    StyleSheet,
    PDFViewer,
} from '@react-pdf/renderer';

const styles = StyleSheet.create({
    page: {
        flexDirection: 'row',
        backgroundColor: '#E4E4E4'
    },
    section: {
        margin: 10,
        padding: 10,
        flexGrow: 1
    }
});

const PDFDoc = () => (
    <Document>
        <Page size="A4" style={styles.page}>
            <View style={styles.section}>
                <Text>Section #1</Text>
            </View>
            <View style={styles.section}>
                <Text>Section #2</Text>
            </View>
        </Page>
    </Document>
);

export default class OpenModal extends Component {
    constructor() {
        super();
        this.state = {
            // state for showing or hiding modal window
            showModal: false,
            isClient: false,
        };
    }

    setIsClient() {
        this.setState({
            isClient: true,
        });
    }

    componentDidMount() {
        this.setIsClient;
    }

    // State changing function for opening modal window
    handleOpenModal = () => {
        this.setState({
            showModal: true,
        });
    };

    render() {
        return (
            <div>
                {/* Button for opening modal window */}
                <button
                    type="button"
                    onClick={this.handleOpenModal}>
                    Open Modal Window
                </button>
                <Modal>
                    <div id="pdf-app">
                        {this.state.isClient && (
                            <PDFViewer>
                                <PDFDoc />
                            </PDFViewer>
                        )}
                    </div>
                </Modal>
            </div>
        );
    }
}

Expected Behavior: I expected this to display the PDF Document.

Desktop:

  • OS: Windows
  • Browser: I am using the Electron framework
  • React-pdf version: 1.6.8

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
diegomuracommented, Jul 21, 2020

At first, I tried to use the ReactPDF.render() method to save the PDF file locally and then display it but this causes my whole application to refresh. Is there any way to prevent this?

Glad to hear this got fixed!

Then, I tried to use the PDFViewer component. However, this gives me the error.

Because of how react-pdf is shipped there are different entrypoints for node and web projects. This is mostly because both some APIs and implementations are different because of the differences of both environments. Electron apps (as well as server side rendered sites) fall kind of in the middle of this. It’s a node app that get’s rendered in a web view. Unfortunately there is no support for this right now. You can try importing react-pdf from @react-pdf/renderer/lib/react-pdf.browser.es.js or @react-pdf/renderer/lib/react-pdf.browser.cjs.js but I can guarantee it will work

0reactions
shunmiancommented, Sep 11, 2021

When using usePDF hook in electron, I also got the similar error usePDF is a web specific API. You're either using this component on Node, or your bundler is not loading react-pdf from the appropriate web build. in production while it works in development. @diegomura any guidance on this?

1

UPDATE: I have changed import { Font, usePDF } from '@react-pdf/renderer' to import { Font, usePDF } from '@react-pdf/renderer/lib/react-pdf.browser.es.js', as @diegomura suggested in previous comment and the problem is solved!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error "render is a Node specific API" when using the module ...
Error : render is a Node specific API. You're either using this method in a browser, or your bundler is not loading react-pdf...
Read more >
Integrating Electron with WebViewer JavaScript PDF library
This guide will help you integrate a free trial of WebViewer into Electron applications on the browser. It will help you clone the...
Read more >
How to Build an Electron PDF Viewer with PDF.js - PSPDFKit
In this post, you saw how to build an Electron PDF viewer with both the PDF.js open source library and our Electron PDF...
Read more >
Why am I getting the error message “Failed to Load PDF ...
The “Failed to Load PDF Document” error message indicates that the web browser you are using, Google Chrome, is trying to open the ......
Read more >
Basic usage - React PDF Viewer
The web worker is loaded via the `workerUrl` parameter: ... The API version "3.1.81" does not match the Worker version "2.6.347". Or. Uncaught...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found