pdf-lib showing letters according to mapping in custom font but doesn't show font when used the actual custom font

43 Views Asked by At

I have been using pdf-lib to modify a pdf with custom font (Hindi font) but it is showing boxes instead of font. but when I am using english text it is showing hindi font according to mapping. for example - in qwerty keyboard "D" letter is equal to hindi letter "क" so when I am using english font with "D" letter it is showing "क" but when I use actual hindi font like when I use "क" it is showing box.

Here is my code -

import { NextResponse } from "next/server";
import { PDFDocument } from "pdf-lib";
import { readFile, writeFile } from "fs/promises";
import path from "path";
import os from "os";
import fontkit from "@pdf-lib/fontkit";

export async function POST(req: Request) {
  try {
    const data = await req.json();

    const filePath = path.join(
      os.homedir(),
      "/Desktop/modify-pdf/public/blank.pdf"
    );
    const fontPath = path.join(
      os.homedir(),
      "/Desktop/aadharify/font/hindi-font.ttf"
    );

    const pdfDoc = await PDFDocument.load(await readFile(filePath));
    const fontFile = await readFile(fontPath);

    pdfDoc.registerFontkit(fontkit);

    const hindiFont = await pdfDoc.embedFont(fontFile);

    const pages = pdfDoc.getPages();
    const firstPage = pages[0];

    firstPage.drawText(data.nameHindi, {
      x: 5,
      y: 800,
      size: 10,
      font: hindiFont,
    });

    const pdfBytes = await pdfDoc.save();

    await writeFile("output.pdf", pdfBytes);

    console.log("data through api call", data);
    return NextResponse.json(data);
  } catch (error) {
    return new NextResponse("Internal Error", { status: 500 });
  }
}
0

There are 0 best solutions below