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.

Documentation: starting from create-react-app

See original GitHub issue

Hey all,

I’m trying to get up and running with react-pdf starting with create-react-app and I have reached a limit in my understanding of how packages like this work.

This is my index.js file

import React from "react";
import ReactDOM from "react-dom";
import ReactPDF, { PDFViewer } from "@react-pdf/renderer";

import { MyDocument } from "./sections/Test";

// ReactPDF.render(<MyDocument />, `${__dirname}/test.pdf`); // I have to uncomment this line

const App = () => (
  <PDFViewer>
    <MyDocument />
  </PDFViewer>
);

ReactDOM.render(<App />, document.getElementById("root"));

Now, I am trying to render PDF previews in the browser so I can render-on-save (for productivity) and then finally build a PDF as part of my npm run build script. This is my understanding of how react-pdf can be put to work.

The above shows the PDF in the PDF Viewer, but only after removing the build line due to error

TypeError: undefined is not a function (near '..._react_pdf_renderer__WEBPACK_IMPORTED_MODULE_2__["default"].renderToFile...')

So, I am sure that my setup is wrong, but I haven’t been able to find documentation as to how to get this render function to work in Node. When I put the function in its own file and try to run it with Node I predictably get an error due to not first using Babel or whatever to compile everything over.

So I am requesting an addendum to the docs that assume the user has no knowledge of React outside of Create React App to get started building PDFs with react-pdf.

Also, I’m on LTS 8, I have no idea if that matters. I can’t find anything in this repo stating what version of Node is to be used. Cheers!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
lukepighetticommented, Nov 30, 2018

For others who want to use react-pdf but have never had to setup a React environment from scratch before, you can use these instructions.

Install babel and a couple presets

npm i -D babel-cli babel-preset-react babel-preset-env

Add this .babelrc file to project directory

{
  "presets": ["env", "react"]
}

Add this to your package.json scripts

    "compile": "babel-node src/render.js"

This assumes a render.js containing the following

import React from "react";
import ReactPDF from "@react-pdf/renderer";
import { MyDocument } from "MyDocument";

ReactPDF.render(<MyDocument />,`${__dirname}/test.pdf`);
0reactions
atherdoncommented, Jan 4, 2019

this package have an old version of babel core

https://github.com/diegomura/react-pdf/blob/master/package.json#L55-L65

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting Started - Create React App
Create React App is an officially supported way to create single-page React applications. It offers a modern build setup with no configuration.
Read more >
Create a New React App
Create React App is a comfortable environment for learning React, and is the best way to start building a new single-page application in...
Read more >
Guides > Create React App - Begin documentation
Build a React app starting with Create React App and an added example API ... This tutorial is an introductory walkthrough of creating...
Read more >
Document Your React Applications The Right Way
A step-by-step guide on properly documenting our React applications · Step 1. Install dependencies # · Step 2. Start Server # · Step...
Read more >
create-react-app/README.md at main - GitHub
Set up a modern web app by running one command. Contribute to facebook/create-react-app development by creating an account on GitHub.
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