Onedoc
Use Onedoc as your primary documents generation and management providers
In this section we will show you how can you generate and host a .pdf using Onedoc’s SDK.
Prerequisites
To get the most out of this guide, you’ll need to:
1. Installation
Want to get started without a project? We have setup a simple CodeSandbox for you to try out.
If you did not have set up a NodeJS project, you can use the following command to do so:
npm init -y
Get the Onedoc Node.js SDK
2. Create a document template
Create a components
directory using the following command:
mkdir components
Then create your document template on components/pdf-template.jsx
.
Your file must export a React component.
import * as React from "react";
export const PDFTemplate = ({ name }) => {
return (
<div>
<h1> Hello {name} !</h1>
</div>
);
};
and optionally, a stylesheet named stylesheet.css
, in the root
directory:
body {
background-color: lightblue;
}
3. Generate your PDF
Create a new file generate.tsx', import the React document template and generate a PDF using your
ONEDOC_API_KEY` and your main React component.
You are almost done ! Run the script using the following command :
npx tsx ./generate.tsx
Stylesheets and other referenced assets should be passed in the asset
parameter. If you are using a framework such as Tailwind, you must submit a compiled stylesheet that can be applied to a HTML file that contains the compiled React component.
You must upload the content of your asset in string or Buffer format in the content
field.
4. Congratulations !
You just created your first PDF using Onedoc Node.js + React SDK. You can now access your private dashboard to preview and download your PDF.