Search code examples
node.jspdflib

why PDF_begin_page_ext: Function must not be called in 'object' scope


the code is read pdf file and return new file copy from source pdf I got the error when compile with nodejs https://github.com/NitroPye/node-pdflib

use nodejs to pdflib

const lib = require('../../pdf');
const pdflib = lib.pdflib;
const PDFLIB = lib.DEFINES;

const pdf = pdflib.PDF_new();

pdflib.PDF_set_option(pdf, "errorpolicy=exception");
pdflib.PDF_set_option(pdf, "hypertextencoding=host");
pdflib.PDF_set_info(pdf, "Creator", "quickreference_ios");
pdflib.PDF_set_info(pdf, "Author", "Thomas Merz");
pdflib.PDF_set_info(pdf, "Title", "mini imposition demo (Objective-C)");

const manual = pdflib.PDF_open_pdi_document(pdf,"ArtworkBlankApp.pdf",0,"")
let row = 0
let col = 0

const endpage = parseInt(pdflib.PDF_pcos_get_number(pdf,manual, "length:pages"));
const font = pdflib.PDF_load_font(pdf, "Helvetica-Bold", 0, "host", "");
for(let pageno = 1; pageno <= endpage; pageno++){
  pdflib.PDF_begin_page_ext(pdf, PDFLIB.a4_width, PDFLIB.a4_height, "topdown")
  const page = pdflib.PDF_open_pdi_page(pdf, manual, pageno, "");
  pdflib.PDF_fit_pdi_page(pdf, page, 0 , PDFLIB.a4_height, "")
  pdflib.PDF_close_pdi_page(pdf, page)
  pdflin.PDF_end_page_ext("")
}
pdflib.PDF_end_document(pdf,"")
pdflib.PDF_close_pdi_document(manual)

How can i fix that


Solution

  • You need to call the PDFlib function PDF_begin_document() before you can create any content in the output document. So putting the following statement before the for loop will do the trick:

    pdflib.PDF_begin_document(pdf, "out.pdf", 0, "")
    

    This will result in a PDF file named "out.pdf" being generated by your program.