Skip to content

Commit e47c4b6

Browse files
committed
Upgrades to Fileforge's API
1 parent 7c39fc1 commit e47c4b6

File tree

99 files changed

+125
-129
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+125
-129
lines changed

docgen/renderPreview.tsx

+28-33
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import { config } from "dotenv";
22

3-
import { Onedoc } from "@onedoc/client";
3+
import { FileforgeClient } from "@fileforge/client";
44
import * as crypto from "crypto";
55
import * as path from "path";
66
import * as fs from "fs";
77
import { fromBuffer } from "pdf2pic";
88
import { glob } from "glob";
99
import { compile, CompileOptions } from "../dist";
1010
import React from "react";
11+
import { pipeline } from "stream/promises";
1112

1213
config({ path: ".env.local" });
1314
config();
1415

15-
const onedoc = new Onedoc(process.env.ONEDOC_API_KEY!);
16+
const ff = new FileforgeClient({
17+
apiKey: process.env.ONEDOC_API_KEY!,
18+
});
1619

1720
export const baseCss = fs.readFileSync(path.join(__dirname, "./base.css"));
1821
export const indexCss = fs.readFileSync(
@@ -29,7 +32,10 @@ export async function renderPreview(
2932
const Component = component;
3033
const Element = <>{Component}</>;
3134

32-
const html = (await compile(Element, compileOptions)) as string;
35+
const html = `<!doctype html><html><head>
36+
<link rel="stylesheet" href="base.css" />
37+
<link rel="stylesheet" href="index.css" />
38+
</head><body>${await compile(Element, compileOptions)}</body></html>` as string;
3339

3440
const hash = crypto.createHash("sha256");
3541
hash.update(html);
@@ -41,45 +47,34 @@ export async function renderPreview(
4147

4248
// If the file doesn't exist, create it by generating the document with Onedoc
4349
if (!fs.existsSync(targetFolder)) {
44-
const { file, info, error } = await onedoc.render({
45-
html,
46-
assets: [
47-
...(useBaseCss
48-
? [
49-
{
50-
path: "base.css",
51-
content: baseCss,
52-
},
53-
]
54-
: [
55-
{
56-
path: "default.css",
57-
content: Buffer.from(`@page { size: A4; }`),
58-
},
59-
]),
60-
{
61-
path: "index.css",
62-
content: indexCss,
63-
},
50+
const file = await ff.pdf.generate(
51+
[
52+
new File([html], "index.html", { type: "text/html" }),
53+
useBaseCss
54+
? new File([baseCss], "base.css", { type: "text/css" })
55+
: new File([`@page { size: A4; }`], "base.css", { type: "text/css" }),
56+
new File([indexCss], "index.css", { type: "text/css" }),
6457
],
65-
save: false,
66-
test: false,
67-
});
68-
69-
if (error) {
70-
throw new Error(`Error rendering the document: ${error}`);
71-
}
58+
{
59+
options: {
60+
host: false,
61+
test: false,
62+
},
63+
}
64+
);
7265

7366
// Create the directory
7467
fs.mkdirSync(targetFolder, { recursive: true });
7568

7669
// Write the HTML to a file
7770
fs.writeFileSync(path.join(targetFolder, "index.html"), html);
7871

79-
const buffer = Buffer.from(file);
72+
await pipeline(
73+
file,
74+
fs.createWriteStream(path.join(targetFolder, "document.pdf"))
75+
);
8076

81-
// Save the buffer to a file called id.pdf
82-
fs.writeFileSync(path.join(targetFolder, "document.pdf"), buffer);
77+
const buffer = fs.readFileSync(path.join(targetFolder, "document.pdf"));
8378

8479
const pdf2pic = fromBuffer(buffer, {
8580
density: 300,

docs/components/compile/compile.mdx

+2-2

docs/components/css/css.mdx

+1-1

docs/components/css/font.mdx

+1-1

docs/components/css/margins.mdx

+1-1

docs/components/footnote/footnote.mdx

+1-1

docs/components/latex/latex.mdx

+2-2

docs/components/markdown/markdown.mdx

+3-3

docs/components/shell/currentpagetop.mdx

+1-1

docs/components/shell/floatbottom.mdx

+1-1

docs/components/shell/nobreak.mdx

+1-1

docs/components/shell/pagebottom.mdx

+1-1

docs/components/shell/pagebreak.mdx

+1-1

docs/components/shell/pagetop.mdx

+1-1

docs/components/signature/field.mdx

+1-1

docs/components/tailwind/tailwind.mdx

+3-3

docs/components/variables/pagenumber.mdx

+2-2

docs/components/variables/pagesnumber.mdx

+1-1

docs/components/variables/runningh1.mdx

+1-1
-24.2 KB
Binary file not shown.
18.6 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

docs/sortedDocs.json

+1-1
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)