Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: ENOENT: no such file or directory, open '/home/amura/generateSlipKylas/.next/server/app/api/generatepdf/data/Helvetica.afm' #1467

Open
anandhelloworld opened this issue Oct 12, 2023 · 6 comments

Comments

@anandhelloworld
Copy link

Description of the problem

I am having a issue when i am trying to create a pdf in api folder of nextjs 13 .

Code sample

import fs from 'fs';
import path, { join } from 'path';
import { NextRequest, NextResponse } from "next/server";

export async function POST(request) {
const res = await request.json()
console.log(res)
const PDFDocument = require('pdfkit');
const doc = new PDFDocument();
doc.pipe(fs.createWriteStream('output.pdf'));
doc
.save()
.moveTo(100, 150)
.lineTo(100, 250)
.lineTo(200, 250)
.fill('#FF3300');
doc.end();

// Register fontkit with PDFDocument
try {

return new NextResponse("pdfBytes", {
  status: 201,
  headers: {
    "Content-Type": "application/json",
    // 'Content-Disposition': 'inline; filename="output.pdf"'
  },
});

} catch (error) {
console.log("error", error)
let error_response = {
status: "error",
message: error.message,
};
return new NextResponse(JSON.stringify(error_response), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
}

ERROR:
Error: ENOENT: no such file or directory, open '/home/amura/generateSlipKylas/.next/server/app/api/generatepdf/data/Helvetica.afm'
at Object.openSync (node:fs:601:3)
at Object.readFileSync (node:fs:469:35)
at Object.Helvetica (webpack-internal:///(rsc)/./node_modules/pdfkit/js/pdfkit.js:5021:19)
at new StandardFont (webpack-internal:///(rsc)/./node_modules/pdfkit/js/pdfkit.js:5057:58)
at PDFFontFactory.open (webpack-internal:///(rsc)/./node_modules/pdfkit/js/pdfkit.js:5349:24)
at PDFDocument.font (webpack-internal:///(rsc)/./node_modules/pdfkit/js/pdfkit.js:5401:37)
at PDFDocument.initFonts (webpack-internal:///(rsc)/./node_modules/pdfkit/js/pdfkit.js:5375:18)
at new PDFDocument (webpack-internal:///(rsc)/./node_modules/pdfkit/js/pdfkit.js:7616:14)
at POST (webpack-internal:///(rsc)/./src/app/api/generatepdf/route.js:19:17)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async eval (webpack-internal:///(rsc)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:253:37) {
errno: -2,
syscall: 'open',
code: 'ENOENT',
path: '/home/amura/generateSlipKylas/.next/server/app/api/generatepdf/data/Helvetica.afm'
}

Your environment

  • pdfkit version:0.13
  • Node version: nextjs 13
@anandhelloworld
Copy link
Author

I tried making a file name data and then added Helvetica.afm font file in it , but doesn't work

@jimwib
Copy link

jimwib commented Oct 13, 2023

I'm having the same issue but my error for the missing file was from a different directory.

To confirm I'm using next 13.5.4 and an api route in the app dir /src/app/api/pdf/route.js

.next/server/vendor-chunks/data/Helvetica.afm

At the top of my pdf api route I added the following to check if the font files exist and if not copy them to where it was moaning about not finding them and it's working for me.

const root = __dirname.split(".next")[0];
const destinationFolder = `${root}.next/server/vendor-chunks/data`;
const dataFolder = `${root}node_modules/pdfkit/js/data`;

const file = `${destinationFolder}/Helvetica.afm`;
const exists = await fileExists(file);

if (exists === false) {
  fs.cp(dataFolder, destinationFolder, { recursive: true }, (err) => {
    if (err) {
      console.error(err);
    }
  });
}```

@bayasdev
Copy link

calling from pages folder worked for me

@anandhelloworld
Copy link
Author

Yes calling from pages worked for me too , but i needed it on server side .

@bayasdev
Copy link

Yes calling from pages worked for me too , but i needed it on server side .

Use API folder inside pages router

@laszlo1337
Copy link

I noticed you're using ESM import statements. So this is probably the same problem as in my case: #1491

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants