ol-th / pdf-img-convert.js

Simple node package to convert a PDF into images.
MIT License
161 stars 38 forks source link

Fillable PDF Forms #44

Open eiden109 opened 1 year ago

eiden109 commented 1 year ago

Loving the package - the only issue I've ran into so far is form fields that are filled out in a PDF are converted into blank fields after the PDF is converted into an image.

I was wondering if anyone knows of any fixes or work arounds for this - or if I'm just doing something wrong lol - thanks in advance!

thomas-robinpowered commented 1 year ago

Also ran into this problem...not sure yet what's going on.

aaron-md commented 1 year ago

Also ran into this problem...not sure yet what's going on.

I've found that you need to flatten the PDF first in order to capture any annotations made to a pdf. This can be done manually with the PDF file or through a different npm package. I use the pdf-lib npm package to flatten the pdf first, and then convert it into an image. Hope this helps!!!!

EDIT: Actually using a different package. See reply below.

thomas-robinpowered commented 1 year ago

@aaron-md maybe you can help me understand what I'm doing wrong. I'm using that library and flattening the pdf but the resulting png file doesn't have the text field.

my minimal code example:

import { convert } from "pdf-img-convert";
import { PDFDocument } from "pdf-lib";

PDFDocument.load(file).then((pdf) => {
  const form = pdf.getForm();

  const textField = form.getTextField("TextBox1");

  textField.setText("Hey, please work.");

  form.flatten();

  pdf.save().then((pdfUint8Array) => {
    const buffer = Buffer.from(pdfUint8Array);
    convert(buffer).then((converted) => {
      writeFileSync("minimal.png", converted[0]);
    });
  });
});
aaron-md commented 1 year ago

@thomas-robinpowered My apologies - I had looked over my old code a bit too quickly and gave you a misleading response. I actually am using the pdf2pic npm package for converting pdf's to images. I believe this package automatically flattens the pdf before converting, so this should solve any annotation issues. Let me know if this helps!!

thomas-robinpowered commented 1 year ago

@aaron-md thank you for getting back to me, I'll give that library a try!

kuang commented 11 months ago

Running into this problem too - is fixing this on the roadmap at all?