Renders a React element to word document with a filepath and returns a top-level document object.
React element
filepath for the document
const SampleDocument = () => (
<Document>
<Text>Hello World</Text>
</Document>
)
render(<SampleDocument />, `${__dirname}/example.docx`)
You can also render the documents straight to memory mapped stream. For example -
import { Component } from 'react'
import { Document, Text } from 'redocx'
class App extends Component {
render() {
return (
<Document>
<Text style={{ color: 'red' }}>Hello World</Text>
</Document>
);
}
}
render(<App />).then((stream) => {
fs.open('sample.docx', 'w+', stream.length, null, (err, fd) => {
if (err) {
console.log(err);
}
fs.write(fd, stream.toBuffer(), (writeErr) => {
if (writeErr) {
console.log(writeErr);
}
console.log('Docx generated and saved to sample.docx');
});
});
});
Wrapper for the child components.
Prop | Type | Default | Description |
---|---|---|---|
align |
string |
left |
align the child components |
children |
Node |
||
style |
object |
{} |
style the child components |
info |
object |
{} |
information about the document such as author, subject, keywords and description of the document |
const DocumentComponent = () => (
<Document
align='center'
style={{ color: 'red', fontSize: 20 }}
info={{ author: 'Nitin', description: 'Sample document' }}
>
Hello World
</Document>
Data that you can pass into info
prop -
{
author: string,
description: string,
keywords: string,
orientation (default is 'portrait'): string,
subject: string,
title: string
}
Creates a new paragraph.
Prop | Type | Default | Description |
---|---|---|---|
align |
string |
left |
align the text |
children |
Node |
||
style |
object |
{} |
style the text |
const TextComponent = () => (
<Text style={{ color: 'mistyrose', bold: true }} align='right'>
Hello World
</Text>
)
Wrapper for the list items (list of dots and numbers).
Prop | Type | Default | Description |
---|---|---|---|
children |
Node |
||
style |
object |
{} |
style for all the list items |
const ListComponent = () => (
<List style={{ fontSize: 40 }}>
<BulletItem>Manchester United</BulletItem>
<BulletItem>Chelsea</BulletItem>
<BulletItem>Liverpool</BulletItem>
</List>
)
Creates a list of dots
Prop | Type | Default | Description |
---|---|---|---|
children |
Node |
||
style |
object |
{} |
style for each list item |
const ListDots = () => (
<List>
<BulletItem style={{ color: 'blue' }}>Item one</BulletItem>
<BulletItem style={{ fontSize: 30 }}>Item two</BulletItem>
<BulletItem style={{ backgroundColor: 'purple' }}>Item three</BulletItem>
</List>
)
Creates a list of numbers
Prop | Type | Default | Description |
---|---|---|---|
children |
Node |
||
style |
object |
{} |
style for each list item |
const ListDots = () => (
<List>
<NumberItem style={{ color: 'blue' }}>Item one</NumberItem>
<NumberItem style={{ fontSize: 30 }}>Item two</NumberItem>
<NumberItem style={{ backgroundColor: 'purple' }}>Item three</NumberItem>
</List>
)
Creates a header for the document
Prop | Type | Default | Description |
---|---|---|---|
align |
string |
left |
align the header |
children |
Node |
||
style |
object |
{} |
style for header |
const HeaderComponent = () => (
<Header style={{ fontSize: 40, bold: true, fontFamily: 'Tahoma' }}>
This is a header
</Header>
)
Creates a footer for the document
Prop | Type | Default | Description |
---|---|---|---|
align |
string |
left |
align the footer |
children |
Node |
||
style |
object |
{} |
style for footer |
const HeaderComponent = () => (
<Footer style={{ fontSize: 40, bold: true, fontFamily: 'Tahoma' }}>
This is a header
</Footer>
)
Draws a horizontal line
const DrawHr = () => <Hr />
Puts a line break
const LineBr = () => <LineBreak />
Puts a page break
const PageBr = () => <PageBreak />
Creates a table
Prop | Type | Default | Description |
---|---|---|---|
headers |
array |
[] |
headings and corresponding style for each heading (array of objects where each object has two properties, value and styles ) |
data |
array |
[] |
data for each heading (array of arrays where each array contains data for each heading) |
style |
object |
{} |
table styles |
const tableStyle = {
tableColWidth: 4261, // Width of each column
tableSize: 24, // Table size
tableColor: 'red', // Content color
tableAlign: 'center', // Align content
borders: true, // Borders
};
const HEADERS = [
{
value: 'Phone',
styles: {
color: 'red',
bold: true,
size: 10
}
},
{
value: 'Capacity',
styles: {
color: 'blue',
bold: true,
size: 10
}
}
]
const DATA = [
['iPhone 7', '128GB'],
['iPhone 5SE', '64GB']
]
const TableComponent = () => (
<Table headers={HEADERS} data={DATA} style={tableStyle} />
)
Adds an image
Prop | Type | Default | Description |
---|---|---|---|
src |
string |
image source | |
align |
string |
left |
align the image |
width |
number |
width of the image | |
height |
number |
height of the image | |
style |
object |
{} | style attributes of the image (height and image) |
const ImageComponent = () => (
<Image src='./xyz/one.png' align='center' width='200' height='200' />
)
A lot of other custom renderers (react-sketchapp, react-tv all have it). It's useful for platform differences within universal components.
import { Platform } from 'redocx'
Platform.OS // 'word'
const obj = {
word: { /** some platform specific code that you want to run **/ }
}
// Then use it like this
Platform.select(obj)
It's still experimental and may change in future.