Closed ghost closed 6 years ago
Ok, well the main issues was I forgot the default
in the class declaration of <TestPrint />
. Should have been export default class TestPrint extends Component {...}
. Anyway, that cleared up the above error, but presented me with another error:
Warning: Please move
inside a PDFViewer or passed to PDFDownloadLink or BlobProvider. Document as root will be deprecated in future versions
Ok, even though it is wrapped in a <PDFDownloadLink>
. I tried adding <PDFViewer>
to the parent and the child, but not at the same time, with the following error:
Uncaught Error: Invalid element of type iframe passed to PDF renderer
The parent looked like this:
<PDFDownloadLink document={
<PDFViewer>
<TestPrint />
</PDFViewer>
} fileName="somename.pdf">
{({ blob, url, loading, error }) => (
loading ? 'Loading document...' : 'Download now!'
)}
</PDFDownloadLink>
The child looked like this:
render() {
return (
<PDFViewer>
<Document>
<Page>
<View>
<Text>Section #1</Text>
</View>
<View>
<Text>Section #2</Text>
</View>
</Page>
</Document>
</PDFViewer>
)
}
My questions still remain from the end of my first post.
One last variation before I call it quits for the day.
Got rid of the <PDFDownlink>
to just use a regular button that changes this.state.print
which when true should trigger ReactPDF.render(<TestPrint />,
${__dirname}/example.pdf)
... no dice. I am getting Uncaught TypeError: _renderer2.default.render is not a function
and I am importing ReactPDF into the component with:
import ReactPDF, { PDFDownloadLink, PDFViewer, Document, Page, Text, View } from '@react-pdf/renderer';
Hey! Thanks for reporting this. I’m on vacations now, answering from my phone so there’s no much I can do to help right now, rather some comments:
Hi, thanks for the quick response! Think I got it now. Needed to remove the <Document>
from the child component and into the parent. Looked like this in the end:
<PDFDownloadLink document={
<Document>
<TestPrint />
</Document>
} fileName="somename.pdf">
{({ blob, url, loading, error }) => (
loading ? 'Loading document...' : 'Download now!'
)}
</PDFDownloadLink>
And for the child component:
import React, { Component } from 'react';
import {
Page,
Text,
View,
} from '@react-pdf/renderer';
export default class TestPrint extends Component {
// renders the search box that filters down the list of positions
render() {
return (
<Page>
<View>
<Text>Section #1</Text>
</View>
<View>
<Text>Section #2</Text>
</View>
</Page>
)
}
}
So it works so far! Going to close the issue for now.
Enjoy your vacation!
This was one of the top hits from google about passing in a valid Document and not a div.
This library is not designed to create a PDF of rendered HTML, IE the physical DOM output of your react.render(). This library is designed to create PDF's using its own components.
OS: Windows 10, but everything is running in Windows Subsystem for Linux (WSL) which is Ubuntu 18.04
React-pdf version: 1.0.0-alpha.18
Description: Frankly, I'm not good at translating the documentation into ES6, so part of them problem.
I have a component that passes several props to child components to render some query results. This is the "web view". I need to generate a printable version, that has a lot of different formatting from the web view (footers, headers, page numbers, page breaks, etc.), so am just wanting to use
<PDFDownloadLInk>
since I don't really need to render the component.This is the parent component where I am calling the
<PDFDownloadLink>
:This does work and prints the document. Sweet.
However, I do not want any of the formatting in this component because it would be an absolute mess. I want to import another component
<TestPrint />
which is where I want all the formatting for the PDF to take place (<Document>
,<Page>
, etc.). Trying to keep it clean and organized.I removed the
const MyDoc
and modify the following:To import this component:
This generates the following area:
How should I be approaching this?
How can I have all the PDF structure in one file and import it into the
<PDFDownloadLink>
?Should I be using
ReactPDF.render()
and if so, how can I render the PDF for printing without rendering it in browser? Is the${__dirname}/example.pdf
a server location or will it automatically download for the user?If it is just a VanillaJS solution, I'll also need to figure out how to pass the props back and forth parent and child components.