Last active
September 30, 2022 09:32
-
-
Save webshared/dd2e257f669b1a5ca06e4f4514326d3b to your computer and use it in GitHub Desktop.
React-Pdf workaround: renders PDF as Blob in browser and display at page + show download link
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { | |
Page, Text, Image, View, Document, StyleSheet, | |
createElement, pdf, PDFRenderer, | |
} from '@react-pdf/core'; | |
import blobStream from 'blob-stream'; | |
const Doc = () => ( | |
<Document> | |
<Page wrap={true}> | |
<Text fixed={true}> | |
~ HELLO PDF RENDERER ~ | |
</Text> | |
</Page> | |
</Document> | |
); | |
class Pdf extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
downloadUrl: null, | |
} | |
this.pdfNode = null; | |
this.setPdfRef = elem => { | |
this.pdfNode = elem; | |
}; | |
this.renderPdf = async () => { | |
if (this.pdfNode) { | |
// Render the PDF here | |
const element = <Doc />; | |
const container = createElement('ROOT'); | |
const node = PDFRenderer.createContainer(container); | |
PDFRenderer.updateContainer(element, node, null); | |
const buffer = await pdf(container).toBuffer(); | |
const stream = buffer.pipe(blobStream()); | |
const url = await new Promise((resolve, reject) => { | |
stream.on('finish', () => { | |
resolve(stream.toBlobURL('application/pdf')); | |
}); | |
stream.on('error', reject); | |
}); | |
this.setState({downloadUrl: url}); | |
this.pdfNode.src = url; | |
} | |
} | |
} | |
componentDidMount() { | |
this.renderPdf(); | |
} | |
render() { | |
return ( | |
<div> | |
{this.state.downloadUrl && ( | |
<div><a href={this.state.downloadUrl} download="file.pdf">Download PDF</a></div> | |
)} | |
<iframe style={{width: 700, height: 800}} ref={this.setPdfRef}> | |
</iframe> | |
</div> | |
); | |
} | |
} | |
export default Pdf; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@rpilev, I got it using the lib
pdfjs-dist
You should be able to improve this logic, but it is a way that I was able to do haha 😅
1. I transformed the document into a url:
2. I transformed the url into a canvas passing the desired page, if you have many pages:
3. I drew this canvas on another canvas that was in html