galkahana / HummusJS

Node.js module for high performance creation, modification and parsing of PDF files and streams
http://www.pdfhummus.com
Other
1.14k stars 169 forks source link

appendPDFPagesFromPDF and modify that file #231

Closed DarkKnight1992 closed 6 years ago

DarkKnight1992 commented 6 years ago

Hi Gal,

Cheers for creating a great library its very useful. Keep up the good work,

I am trying to append a pdf to an existing createWriterToModify file and file is appended without problem but when i try to startcontext on that page i can't do that maybe i missing something can you help me asap ?

error in console

TypeError: context not created, page index is either wrong, or page is null
    at Object.addPage (E:\Projects\ycbbilling\src-electron\electronApp\utils\pdfWriter.js:55:26)
    at exports.createMultiplePDF (E:\Projects\ycbbilling\src-electron\electronApp\utils\generatepdf.js:92:7)
    at recs.reduce.then (E:\Projects\ycbbilling\src-electron\background.js:202:42)
    at <anonymous>

Error in Hummus Log

[ 03/01/2018 00:38:54 ] AbstractContentContext::PDFModifiedPage, null page object

Here's my code

export default function(opts) {
  let cfg, ctx, pageModifier, pdfWriter;
  pdfWriter = Hummus.createWriterToModify(opts["in"], {
    modifiedFilePath: opts.out,
    log: opts.log
  });
  pageModifier = new Hummus.PDFPageModifier(pdfWriter, opts.pageNumber);
  ctx = pageModifier.startContext().getContext();
  cfg = {
    font: pdfWriter.getFontForFile(__dirname + "/SourceSansPro-Regular.ttf"),
    size: 14,
    colorspace: 'gray',
    color: 0x00
  };
  return {
    write: function(x, y, txt) {
      ctx.writeText(txt, x, y, cfg);
      return this;
    },
    end: function() {
      pageModifier.endContext().writePage();
      return pdfWriter.end();
    },
    cfg: function(cfgIn) {
      cfg = merge(cfg, cfgIn);
      if (cfg.fontPath) {
        cfg.font = pdfWriter.getFontForFile(cfg.fontPath);
        delete cfg.fontPath;
      }
      return this;
    },
    image: function(x, y, path, opts) {
      if (opts == null) {
        opts = {};
      }

      ctx.drawImage(x, y, path, opts);
      return this;
    },
    page: function(nPage) {
      pageModifier.endContext().writePage();
      pageModifier = new Hummus.PDFPageModifier(pdfWriter, nPage);
      ctx = pageModifier.startContext().getContext();
      return this;
    },
    addPage: (nPage) => {
      pdfWriter.appendPDFPagesFromPDF(opts["in"]);
      pageModifier = new Hummus.PDFPageModifier(pdfWriter, nPage);
      ctx = pageModifier.startContext().getContext();
      return this;
    },
    endPage: () => {
      pageModifier.endContext().writePage();
      return this;
    },
    restoreCfg: function() {
      cfg = {
        font: pdfWriter.getFontForFile(__dirname + "/SourceSansPro-Regular.ttf"),
        size: 14,
        colorspace: 'gray',
        color: 0x00
      };
      return this;
    }
  };
};

This is here its triggered

  let page = pages[0];

    try {
      writeFile(textaxis[0], page[0]);

      if(page.length > 1){
        writeFile(textaxis[1], page[1]);
      }
      pdf.endPage();
    } catch(e) {
      logger.error(`pdf written`, e);
    } 

    pdf.addPage(1);
chunyenHuang commented 6 years ago

You will need to create a new PDFPageModifier for the new page. The usage is more like

const hummus = require('hummus');
const pdfWriter = hummus.createWriterToModify(INPUT_FILE, {
    modifiedFilePath: OUTPUT_FILE
});

for (let i = 0; i < PAGE_COUNT; i++) {
    const pageModifier = new hummus.PDFPageModifier(pdfWriter, i);
    const cxt = pageModifier.startContext().getContext();
    // DO your editing here
    pageModifier.endContext().writePage();
};

pdfWriter.end();

If this one does not work for you, you may have to provide your full codes since the current trigger code does not match your function.

If you want to wrap hummus with your own class, you will probably need to take care of pausePageContentContext later. Example

You can also take a look for other hummus wrapping library for some ideas link

DarkKnight1992 commented 6 years ago

Thanks @chunyenHuang, but issue has after appending the new file that i can't get context so i got away with doing something else.

First i created the no. of pages i needed for the pdf, by appending the existing pdf over and over again. Then i started pdf writer for it and had no issues with the context.

I think i had the context problem since when i intialized the writer it has only page and after appending the page the writer did not register the new page to be modified

JCMais commented 5 years ago

Had same issue, this occurs if you appendPDFPagesFromPDF from a pdf file to an empty one, and right after try to create a PDFPageModifier on some of the appended pages.

Is that expected behaviour @galkahana ?