vbuch / node-signpdf

Simple signing of PDFs in node.
MIT License
730 stars 178 forks source link

TypeError: pdf.ref is not a function #237

Closed rafliher closed 7 months ago

rafliher commented 8 months ago

Some error raised when trying so sign a pdf in form of bytes and p12Signer

import signpdf from '@signpdf/signpdf';
import { P12Signer } from '@signpdf/signer-p12';
import { pdfkitAddPlaceholder } from '@signpdf/placeholder-pdfkit010';
import { SUBFILTER_ETSI_CADES_DETACHED } from '@signpdf/utils';

const SigningPage: React.FC = () => {
  const [pdfContent, setPdfContent] = useState<string>('');
  const [signer, setSigner] = useState<any>();

  useEffect(() => {
    axios.get(
      `${process.env.REACT_APP_API_SERVER_URL}/api/signature/getp12`, {
          headers: {
            "Content-Type": "application/json",
            Authorization: `Bearer ${authorizationCookie}`,
          },
      }
    ).then(async (response) => {
      const p12b64 = response.data.data;
      const p12Bytes = decode(p12b64)
      setSigner(new P12Signer(p12Bytes));
    }).catch((error) => {
    });
  }, []);

i can obtain a valid P12Signer instance

    const pdfBytes = await pdfDoc.save();
    const pdfB64 = _arrayBufferToBase64(pdfBytes);

    let pdfBuffer = Buffer.from(pdfBytes);

    const pdfToSign = pdfkitAddPlaceholder({
      pdf: pdfBytes,
      pdfBuffer: pdfBuffer,
      reason: 'signing',
      contactInfo: '',
      name: '',
      location: '',
      subFilter: SUBFILTER_ETSI_CADES_DETACHED,
    });

    const signedPdf = await signpdf.sign(pdfBuffer, signer);
    console.log(signedPdf);

trying to sign it, an error was raised image

vbuch commented 7 months ago

Hi @rafliher, If you look at the types of pdfkitAddPlaceholder you will see that the pdf parameter is supposed to be a PDFDocument. That is an instance of pdfkit. So it would work if you give it pdfDoc (before saving) instead of pdfBytes. I don't know why you thought it should be base64 encoded.