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

Helvetica.afm file missing in Next.js #1549

Open
james-beston opened this issue Sep 10, 2024 · 5 comments
Open

Helvetica.afm file missing in Next.js #1549

james-beston opened this issue Sep 10, 2024 · 5 comments

Comments

@james-beston
Copy link

james-beston commented Sep 10, 2024

Bug Report

No Helvetica.afm file is being created when I use Next.js locally.

Description of the problem

I have to manually create a) the data folder and b) the Helvetica.afm file in .next/server/vendor-chunks every time I start the project.

Code sample

import { NextResponse } from 'next/server';
import PDFDocument from 'pdfkit';

export async function GET() {
  // Create a new PDF document
  const doc = new PDFDocument();

  // Create a buffer to store the PDF data
  const pdfBuffer: Buffer = await new Promise((resolve, reject) => {
    const buffers: Uint8Array[] = [];
    doc.on('data', buffers.push.bind(buffers));
    doc.on('end', () => {
      const pdfData = Buffer.concat(buffers);
      resolve(pdfData);
    });

    // Add content to the PDF document
    doc.fontSize(25).text('Hello, world!', 100, 100);
    doc.fontSize(12).text('This PDF was generated server-side with pdfkit in Next.js.', 100, 150);

    // Finalize the PDF
    doc.end();
  });

  // Return the PDF as a response
  return new NextResponse(pdfBuffer, {
    headers: {
      'Content-Type': 'application/pdf',
      'Content-Length': pdfBuffer.length.toString(),
      'Content-Disposition': 'inline; filename="output.pdf"', // or 'attachment;' for download
    },
  });
}

Your environment

  • pdfkit version: 0.15.0
  • Node version: 20.12.2
  • Next version: 14.2.9
  • React version: 18.3.1
@marcoripa96
Copy link

did you find a solution? I also have the same error

@Ftwrr
Copy link

Ftwrr commented Oct 2, 2024

same here

@marcoripa96
Copy link

marcoripa96 commented Oct 2, 2024

The correct solution for the current version of next is declaring pdfkit as a serverComponentExternalPackage in the nextjsconfig:

const nextConfig = {
  experimental: {
    serverComponentsExternalPackages: ["pdfkit"],
  },
};

@akulich
Copy link

akulich commented Oct 31, 2024

The correct solution for the current version of next is declaring pdfkit as a serverComponentExternalPackage in the nextjsconfig:

const nextConfig = {
  experimental: {
    serverComponentsExternalPackages: ["pdfkit"],
  },
};

This "fix" worked for me on Next.js 14.2.16.

Thank you!

@akulich
Copy link

akulich commented Nov 1, 2024

The correct solution for the current version of next is declaring pdfkit as a serverComponentExternalPackage in the nextjsconfig:

const nextConfig = {
  experimental: {
    serverComponentsExternalPackages: ["pdfkit"],
  },
};

This "fix" worked for me on Next.js 14.2.16.

Thank you!

Note that in Next.js 15 serverComponentsExternalPackages seemed to become a production feature as tstikvoort also mentioned in #1467. Here the docs.

So you use:

const nextConfig = {
  serverExternalPackages: ["pdfkit"],
}

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