Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/danomatic/react-pdf-html

Render HTML in react-pdf
https://github.com/danomatic/react-pdf-html

Last synced: 3 months ago
JSON representation

Render HTML in react-pdf

Awesome Lists containing this project

README

        

# react-pdf-html

`` component for [react-pdf](https://github.com/diegomura/react-pdf/)

- Support for CSS via `` tags and `style` attributes (limited to `Style` properties supported by `react-pdf`)
- [Browser CSS defaults](https://www.w3schools.com/cssref/css_default_values.asp) with option for [style reset](https://meyerweb.com/eric/tools/css/reset/)
- Basic `<table>`(attempted using flex layouts) `<ul>` and `<ol>` support
- Ability to provide custom renderers for any tag
- Support for inline `<style>` tags and remote stylesheets (using fetch)

## How it Works

1. Parses the HTML string into a JSON tree of nodes using [node-html-parser](https://github.com/taoqf/node-html-parser)
2. Parses any `<style>` tags in the document and `style` attributes using [css-tree](https://github.com/csstree/csstree)
3. Renders all nodes using the appropriate `react-pdf` components, applying cascading styles for each node as an array passed to the `style` prop:
- block/container nodes using `<View>`
- inline/text nodes using `<Text>`, with appropriate nesting and collapsing of whitepace
- `<img>` nodes using `<Image>`
- `<a>` nodes using `<Link>`

## Installation

```bash
npm i react-pdf-html
```

OR

```bash
yarn add react-pdf-html
```

## Usage

```tsx
import Html from 'react-pdf-html';

const html = `<html>
<body>
<style>
.my-heading4 {
background: darkgreen;
color: white;
}
pre {
background-color: #eee;
padding: 10px;
}

Heading 1


Heading 2


Heading 3


Heading 4



Paragraph with bold, italic, underline,
strikethrough,
and all of the above



Paragraph with image and
link





  • Unordered item

  • Unordered item



  1. Ordered item

  2. Ordered item








Text outside of any tags



Column 1
Column 2
Column 3




Foo
Bar
Foobar


Foo
Bar


Some longer thing
Even more content than before!
Even more content than before!





function myCode() {
const foo = 'bar';
}


{`
.heading4 {
background: darkgreen;
color: white;
}
pre {
background-color: #eee;
padding: 10px;
}`}

Heading 1


Heading 2


...