1.0.21 • Published 7 months ago

express-react-pdf v1.0.21

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Base Root

For simple usage, a base root context element and it's associated middleware are provided as BaseRootContext and basePDFGenerator

Usage

Certificate Element

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

export const Certificate = ({ name, age }: CertificateProps) => {
  return (
    <Page size="A4" orientation="landscape">
      <View>
        <Text>
          {name} - {age}
        </Text>
      </View>
    </Page>
  );
};

Endpoint

import { ProgramCertificate } from "./template";
import { basePDFGenerator } from "express-react-pdf";

router.get(
  "/certificate",
  basePDFGenerator(Certificate, (req) => ({
    name: req.query.name,
    age: req.query.age,
  }))
);

Builder

The pdfMiddlewareBuilder can be used to create react-pdf middleware for your express server. This supports defining a new root context, as well as how to extract data from your request to populate your root.

Example - Integrating Other Frameworks

Note: this will soon be published as a package

Custom root contexts can be defined to integrate other frameworks, for example the i18Next translation library

Root Context

export const I18NextRootContext = ({
  translate,
  children,
}: I18NextRootContextProps & PropsWithChildren) => {
  return (
    <I18nextProvider i18n={translate}>
      <Document>{children}</Document>
    </I18nextProvider>
  );
};

Middleware

export const i18NextPDFMiddleware = pdfMiddlewareBuilder(
  I18NextRootContext,
  (req) => ({
    translate: req.i18n,
  })
);
1.0.21

7 months ago

1.0.20

1 year ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

0.1.13

1 year ago

0.1.12

1 year ago