react-pdf

使用React创建PDF文件。(Create PDF files using React.)

  • Owner: diegomura/react-pdf
  • Platform: Linux, Mac, Windows, BSD, Cross-platform, Solaris, Unix-like
  • License:: MIT License
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

用于在浏览器、移动设备和服务器上创建PDF文件的React渲染器。

如何安装

 yarn add @react-pdf/renderer

工作原理

import React from 'react';
import { Document, Page, Text, View, StyleSheet } from '@react-pdf/renderer';

// Create styles
const styles = StyleSheet.create({
  page: {
    flexDirection: 'row',
    backgroundColor: '#E4E4E4'
  },
  section: {
    margin: 10,
    padding: 10,
    flexGrow: 1
  }
});

// Create Document Component
const MyDocument = () => (
  <Document>
    <Page size="A4" style={styles.page}>
      <View style={styles.section}>
        <Text>Section #1</Text>
      </View>
      <View style={styles.section}>
        <Text>Section #2</Text>
      </View>
    </Page>
  </Document>
);

Web. 在DOM中呈现

import React from 'react';
import ReactDOM from 'react-dom';

ReactDOM.render(<MyDocument />, document.getElementById('root'));

Node. 保存在文件中

import React from 'react';
import ReactPDF from '@react-pdf/renderer';

ReactPDF.render(<MyDocument />, `${__dirname}/example.pdf`);

Native. 在手机中呈现

即将推出

示例

对于每个示例,请尝试打开 output.pdf 以查看结果。

要运行这些示例,首先克隆该项目并安装依赖项:

git clone https://github.com/diegomura/react-pdf.git
cd react-pdf
yarn install

然后,运行 yarn example - &lt; example-name&gt;

 yarn example -- fractals

贡献者

这个项目的存在要感谢所有贡献的人。 [贡献]

赞助商

感谢所有赞助商![成为sponsors](https://opencollective.com/react-pdf#sponsors)

支持者

谢谢你们所有的支持者! [成为支持者](https://opencollective.com/react-pdf#backer)

许可证

麻省理工学院© Diego Muracciole

Overview

Name With Ownerdiegomura/react-pdf
Primary LanguageJavaScript
Program languageJavaScript (Language Count: 3)
PlatformLinux, Mac, Windows, BSD, Cross-platform, Solaris, Unix-like
License:MIT License
Release Count653
Last Release Name@react-pdf/types@2.5.0 (Posted on 2024-04-25 11:41:50)
First Release Namev0.2.0 (Posted on 2017-06-21 23:12:36)
Created At2016-10-18 21:33:58
Pushed At2024-04-27 03:28:23
Last Commit At2024-04-25 13:39:13
Stargazers Count14.1k
Watchers Count101
Fork Count1.1k
Commits Count1k
Has Issues Enabled
Issues Count1715
Issue Open Count439
Pull Requests Count627
Pull Requests Open Count27
Pull Requests Close Count147
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

How to install

yarn add @react-pdf/renderer

How it works

import React from 'react';
import { Document, Page, Text, View, StyleSheet } from '@react-pdf/renderer';

// Create styles
const styles = StyleSheet.create({
  page: {
    flexDirection: 'row',
    backgroundColor: '#E4E4E4'
  },
  section: {
    margin: 10,
    padding: 10,
    flexGrow: 1
  }
});

// Create Document Component
const MyDocument = () => (
  <Document>
    <Page size="A4" style={styles.page}>
      <View style={styles.section}>
        <Text>Section #1</Text>
      </View>
      <View style={styles.section}>
        <Text>Section #2</Text>
      </View>
    </Page>
  </Document>
);

Web. Render in DOM

import React from 'react';
import ReactDOM from 'react-dom';
import { PDFViewer } from '@react-pdf/renderer';

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

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

Node. Save in a file

import React from 'react';
import ReactPDF from '@react-pdf/renderer';

ReactPDF.render(<MyDocument />, `${__dirname}/example.pdf`);

Examples

For each example, try opening output.pdf to see the result.

To run the examples, first clone the project and install the dependencies:

git clone https://github.com/diegomura/react-pdf.git
cd react-pdf
yarn install

Then, run yarn example -- <example-name>

yarn example -- fractals

Contributors

This project exists thanks to all the people who contribute. (CONTRIBUTING.md).

Sponsors

Thank you to all our sponsors! [Become a sponsors]

Backers

Thank you to all our backers! [Become a backer]

License

MIT © Diego Muracciole

FOSSA Status


To the top