dmk99 / react-pdf-table

Storybook Available
https://dmk99.github.io/react-pdf-table
MIT License
152 stars 62 forks source link

How show non-latin data? #25

Closed DFilyushin closed 3 years ago

DFilyushin commented 4 years ago

import { Table, TableCell, TableHeader, TableBody, DataTableCell } from '@david.kucsai/react-pdf-table'

For react-pdf added font register: Font.register({ family: 'Arial', src: '/fonts/Arial-Regular.ttf' }); Font.register({ family: 'Arial-Bold', src: '/fonts/Arial-Bold.ttf' });

<Table data={[ {seller: 'Поставщик', customer: 'Покупатель'} ]}>

Поставщик Покупатель
                        <TableBody>
                            <DataTableCell getContent={(r) => r.seller}/>
                            <DataTableCell getContent={(r) => r.customer}/>
                        </TableBody>
                    </Table>

screen

dmk99 commented 4 years ago

Hi,

Unfortunately I have not tested this with non latin fonts. Thank you for bringing it to my attention. I will investigate how we can best support it.

dmk99 commented 3 years ago

If this is still relevant to you, this can actually be achieved by setting up the style for the DataTableCell. Below is an example that will work with your text:

import {useState} from "react";
import {PdfContainer} from "../PdfContainer";
import * as React from "react";
import {DataTableCell, Table, TableBody, TableCell, TableHeader} from "../../../src";
import {Font, Text} from "@react-pdf/renderer";

const Roboto = require("../../resources/Roboto-Regular.ttf");
const RobotoBold = require("../../resources/Roboto-Bold.ttf");

export default () => {
    const [items, setItems] = useState<string[]>([
        "Поставщик Покупатель",
        "γεια"
    ]);

    Font.register({
        family: 'Roboto', fonts: [
            {src: Roboto},
            {src: RobotoBold, fontWeight: "bold"}
        ]
    });

    return (
        <PdfContainer>
            <Table
                data={items}
            >
                <TableHeader>
                    <TableCell>Text</TableCell>
                </TableHeader>
                <TableBody>
                    <DataTableCell getContent={c => <Text style={{fontFamily: "Roboto"}}>{c}</Text>} />
                </TableBody>
            </Table>
        </PdfContainer>
    )
}

Note that I am setting the fontFamily here: <DataTableCell getContent={c => <Text style={{fontFamily: "Roboto"}}>{c}</Text>} />

If you set yours to Arial then it should work.

image