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

drawImage() the image will be blocked if the param is large #322

Closed akalittle closed 5 years ago

akalittle commented 5 years ago

Description

I tried to modify an existing pdf from buffer. With my code below, i can get another pdf file ./result.pdf just the same as origin one.

  //  this is buffer like <Buffer 25 50 44 46 2d 31 ... 6d 29 0a 2f 50 72 6f 64 ... > 
  const buffer = _file.buffer;
  // const readStream = fs.createReadStream(buffer);
  const inStream = new hummus.PDFRStreamForBuffer(buffer);
  const outStream = new hummus.PDFWStreamForFile('./result.pdf');
  const pdfWriter = hummus.createWriterToModify(inStream, outStream);

  let pageModifier = new hummus.PDFPageModifier(pdfWriter, 0, true);
  pageModifier.startContext().getContext().writeText(
    'Test Text2222222',
    300, 500,
    { font: '/Library/Fonts/Arial.ttf', size: 18, colorspace: 'gray', color: 0x00 }
  );
  pageModifier.endContext().writePage();
  pdfWriter.end();
  // inStream.close(() => {
  //   console.log('inStream done');
  // });
  // outStream.close();
akalittle commented 5 years ago

This is caused by the font. After i set

{font:pdfWriter.getFontForFile(__dirname + '/mydirectory/fonts/Couri.ttf'),size:14,colorspace:'gray',color:0x00}

It worked!

But i found another issus that

  let pdfWriter = hummus.createWriterToModify(
    new hummus.PDFRStreamForBuffer(buffer),
    new hummus.PDFStreamForResponse(outStreamT)
  );

  let pageModifier = new hummus.PDFPageModifier(pdfWriter, 0);

  var imageOptions = {
    transformation: [0.5, 0, 0, 0.5, 0, 0],
    index: 1000
  };

  pageModifier
    .startContext()
    .getContext()
    // .drawRectangle(0, 0, 100, 40, pathFillOptions)
    //   parameters top left /filepath
    .drawImage(100, 800, __dirname + '/png/transfer.png', imageOptions);

  pageModifier.endContext().writePage();
  pdfWriter.end();

It confuses that the params in imageOptions. I have no idea of transformation array. What do the each params means?

and after i use .drawImage() I know from the wiki the first two params are the location. However i tried. When i set the param more than about 850 the image will lost.

akalittle commented 5 years ago

image

This is an example. If i set the first two params of drawImage() bigger. The image will cant be seen

chunyenHuang commented 5 years ago

https://github.com/galkahana/HummusJS/wiki/Show-images#simple-image-drawing

maybe you can try with the object instead

{
  transformation: {
    width:100,
    height:100,
    proportional:true
  }
}
akalittle commented 5 years ago

@chunyenHuang Thanks~ I tried with the object instead, it still doesnt work. However, i tried to clone your hummusRecipe and do some change. To support new HummusRecipe(buffer) receive buffer; And the result return is also buffer. Below is usage. Wondering if i could create a pr to ur repo.

  const pdfDoc = new HummusRecipe(buffer);
  const result = pdfDoc
    .editPage(5)
    .image('./png/google.png', 100, 230, {
      width: 100,
      keepAspectRatio: true,
      opacity: 0.7
    })
    .endPage()
    .endPDF();
chunyenHuang commented 5 years ago

PR is welcome, but I think your issue should be able to be resolved by the HummusJS. My repo is just a wrapper on top of HummusJS.