Skip to content

Commit

Permalink
fix: Local files paths for Vercel (#1016)
Browse files Browse the repository at this point in the history
  • Loading branch information
Betree authored Apr 15, 2024
1 parent ff78e8d commit bd15060
Show file tree
Hide file tree
Showing 29 changed files with 15 additions and 8 deletions.
6 changes: 6 additions & 0 deletions lib/file-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import fs from 'fs';
import path from 'path';

export const readFileSyncFromPublicStaticFolder = (filePath: string): Uint8Array => {
return fs.readFileSync(path.join(process.cwd(), 'public/static', filePath));
};
4 changes: 2 additions & 2 deletions lib/pdf-lib-utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fs from 'fs';
import { get, isNil } from 'lodash';
import { scaleValue } from './math';
import fontkit from 'pdf-fontkit';
import { PDFDocument, PDFField, PDFFont, PDFForm, PDFHexString, PDFTextField, TextAlignment } from 'pdf-lib';
import { allCharsValid } from './string-utils';
import { readFileSyncFromPublicStaticFolder } from './file-utils';

const SIGNATURE_FONT_BYTES = fs.readFileSync('resources/fonts/JustMeAgainDownHere-Regular.ttf');
const SIGNATURE_FONT_BYTES = readFileSyncFromPublicStaticFolder('fonts/JustMeAgainDownHere-Regular.ttf');

export const logAllFieldsFromPDFForm = (pdfForm) => {
for (const field of pdfForm.getFields()) {
Expand Down
8 changes: 4 additions & 4 deletions lib/tax-forms/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fs from 'fs';
import { fillW9TaxForm } from './w9';
import { fillW8BenTaxForm } from './w8-ben';
import { PDFDocument, PDFFont } from 'pdf-lib';
import { fillW8BenETaxForm } from './w8-ben-e';
import { readFileSyncFromPublicStaticFolder } from '../file-utils';

type TaxFormType = 'W9' | 'W8_BEN' | 'W8_BEN_E';

Expand All @@ -17,15 +17,15 @@ export const TAX_FORMS: Record<
}
> = {
W9: {
bytes: fs.readFileSync('resources/tax-forms/fw9.pdf'),
bytes: readFileSyncFromPublicStaticFolder('tax-forms/fw9.pdf'),
fillPDF: fillW9TaxForm,
},
W8_BEN: {
bytes: fs.readFileSync('resources/tax-forms/fw8ben.pdf'),
bytes: readFileSyncFromPublicStaticFolder('tax-forms/fw8ben.pdf'),
fillPDF: fillW8BenTaxForm,
},
W8_BEN_E: {
bytes: fs.readFileSync('resources/tax-forms/fw8bene.pdf'),
bytes: readFileSyncFromPublicStaticFolder('tax-forms/fw8bene.pdf'),
fillPDF: fillW8BenETaxForm,
},
} as const;
Expand Down
5 changes: 3 additions & 2 deletions pages/api/tax-form/[filename].ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { PDFDocument, StandardFonts } from 'pdf-lib';
import fs from 'fs';
import fontkit from 'pdf-fontkit';
import { TAX_FORMS, isValidTaxFormType } from '../../../lib/tax-forms';
import { getFullName } from '../../../lib/tax-forms/utils';
import { flattenForm } from '../../../lib/pdf-lib-utils';
import { allCharsValid } from '../../../lib/string-utils';
import { readFileSyncFromPublicStaticFolder } from '../../../lib/file-utils';

const MAIN_FONT_BYTES = fs.readFileSync('resources/fonts/NanumGothic-Regular.ttf');
const MAIN_FONT_BYTES = readFileSyncFromPublicStaticFolder('fonts/NanumGothic-Regular.ttf');

export default async function handler(req, res) {
// Get values from query
Expand Down Expand Up @@ -76,5 +76,6 @@ export default async function handler(req, res) {
const pdfBytes = await pdfDoc.save();
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Access-Control-Allow-Origin', '*'); // TODO
res.setHeader('cache-control', 'no-cache');
res.send(Buffer.from(pdfBytes));
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit bd15060

Please sign in to comment.