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

How do I change context orientation? #143

Open yinso opened 7 years ago

yinso commented 7 years ago

Hi,

I'm trying to modify an existing PDF file, which happens to be oriented to landscape mode.

When I run the sample code below.

  var writer = hummus.createWriterToModify('test.pdf', {
    modifiedFilePath: 'test1.pdf'
  });
  var modifier = new hummus.PDFPageModifier(writer, 0);
  var arialFont = writer.getFontForFile('./fonts/arial.ttf');
  var ctx = modifier.startContext().getContext().writeText('Test me out', 0, 0, {
    font: arialFont,
    size: 100,
    colorspace: 'rgb',
    color: 0x00aa00
  });
  modifier.endContext().writePage();
  writer.end();

It turns out that the text Test me out gets written to the top left corner vertically, which is of a different orientation from the rest of the page, instead of the expected bottom left corner horizontally.

Is there a way for me to 1) detect the orientation of the page, and/or 2) create the modify context with the same orientation as the page, and/or 3) arbitrarily change the orientation of the context?

Thanks for your help.

galkahana commented 7 years ago

the shortest path to success is o add true as an additional parameter to PDFPageModifier constructor. This will kill any side effects of the existing content, and you can add your content being sure that you control the matrix, color or whatnot.

make this:
var modifier = new hummus.PDFPageModifier(writer, 0); become this:
var modifier = new hummus.PDFPageModifier(writer, 0,true);

yinso commented 7 years ago

Hi,

I tried this on the test.pdf mentioned in #144 and it works, however, for my original source file it still appears to have the same issue, so perhaps my original source file wasn't in landscape mode.

As the original source pdf is confidential in nature, I cannot just link it here. Is there another way of providing it to you? Thanks.

galkahana commented 7 years ago

i'm not sure that i understand the problem then. Let me describe what i understand in my words, so you can either confirm, or tell me what's different from what you see:

  1. The page appears landscape, meaning, the horizontal dimension is longer than the vertical dimension. note sure i understand "oriented'.
  2. existing graphics on the page appears horizontal - meaning, if there's text its written along the longer horizontal dimension.
  3. when adding text with page modifier, although placde in 0,0, you see it in 0,pageHeight. in addition - it is "vertical". i guess that you mean it's rotated - so that it's running along the vertical shorter edge of the page.

well. if that's true than it goes against my understanding on how things work in PDF files in general, and i will definitely need the file. either this, or you can debug this and get to tell me what's going on. or just rotate the text to be the way that you want it to be using the cm operator.

you can send me the file via gal.kahana@hotmail.com. if you deem it necessary i can also sign an nda.

yinso commented 7 years ago

Yes your understanding is correct. I agree it looks very weird and given that I only have high level knowledge of PDF I am not sure how I would go about debugging at this point, so definitely highly appreciative of you helping to look into this.

One interesting side point - based on what you are saying, is portrait vs landscape as simple as a comparison of the width vs the height and see which side is larger? I.e. is there no place in PDF that specify the orientation in terms of a default rotation, i.e. does x grow on the width side or the height side?

I'm sending the file to your email address in the next few minutes. Again - thank you very much for the help to look into this.

galkahana commented 7 years ago

ok. got the file. everything is actually fine and works as expected actually. the page is using a "rotate" parameter that is set to 90. This means that the whole page is rotated in 90 degrees - contents, dimensions the lots.

If you want to align anything that you'll add to the rotated form of the page, you'll have to take that into account, or change the page orientation.

you can get that info with page parsing: https://github.com/galkahana/HummusJS/wiki/Parsing#page-info-objects