Render custom HTML markup inside pdf document
See original GitHub issueHi all, I am trying to render HTML inside the <View>/<Text>
component while rendering the PDF inside the <PDFViewer>
using a Template.
I’d like to render my custom HTML code shown below in my pdf
<p><span style="font-size: 24px;">Pre Interview Notes</span></p><p><br></p><p><strong style="font-size: 14px;"><u>This is a test Pre Interview Notes:</u></strong></p><p><br></p><p><em style="font-size: 14px;">The Guest requires a wheel chair for the show</em></p><p><br></p><p><br></p>
Environment
- Browser [e.g. Chrome ]:
- React-PDF version [1.6.8]:
- React version [e.g. 16.8.6]:
Issue Analytics
- State:
- Created 3 years ago
- Reactions:23
- Comments:20
Top Results From Across the Web
Render custom HTML markup inside pdf document using ...
You will need to parse the HTML markup and re-create the output using the React-PDF components. I'm working on something similar now, except ......
Read more >Render custom HTML markup inside pdf document using ...
Coding example for the question Render custom HTML markup inside pdf document using react-pdf/render library-Reactjs.
Read more >Render HTML to PDF | Documents for PDF .NET Edition
GcPdf allows you to render HTML content to a PDF document easily. Learn how to render webpages, HTML strings or even URLs to...
Read more >How to Create Highly Customized PDF from URL Web Page
Standard URL content to PDF rendering involves only document format conversion. Free edit PDF tools render source HTML code to PDF and don't...
Read more >Custom PDF Rendering in JavaScript with Mozilla's PDF.Js
If your browser doesn't support Web Workers there's no need to worry as pdf.js contains all the code necessary to parse and render...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@thismax and @brxshncy I just published it to NPM:
https://www.npmjs.com/package/react-pdf-html
@stevenbrown-85 it’s a bit more involved than a basic example but I can give you some pointers.
HTML and the mirror react-pdf code are both tree structures, so you need two different functions to walk/save the tree and then rebuild it. You’re looking at recursion to do this.
As you are using tinyMCE, you’re in luck as you can limit the html structures that can be in your tree, you don’t need to do every single HTML element. One thing to note is that tinyMCE
onEditorChange
event just provides a list of HTML elements, so you need to provide a ‘wrapper’ parent element for the below to work. I use a document fragment.Look at this function: dom-to-json, which is where I started. The tree structure is preserved by using a
childNodes
array of sub-nodes. So you start with a document fragment with all the tinyMCE elements aschildNodes
.Then you will need to write your own json-to-pdf function. Mine has an entry point that looks like this
Note that I do some intermediate processing, so there’s a crucial bit missing from this code, which is how to process the parent fragment element. You would do this by
case 11
, where you would, for example, create the react-pdfPage
element using a routine like the following code sample and then all thechildNodes
accordingly.To rebuild, you use
React.createElement
for each node, so for a simpleh1
example:That should get you started with basic text. Lists and tables are a bit tougher to get right.
Styles need to be converted from CSS to something understood by react-pdf. TinyMCE does most of the styling with
span
elements.If you have all the default fonts from tinyMCE, they will need to be loaded from somewhere. Images are also a pain as they have CORS issues.
Hope that helps a bit.
There’s a big project potentially for react-pdf that converts any HTML string to pdf.
Tom