Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draw_image always results in a 300x300 image #17

Open
JasonBarnabe opened this issue Jun 2, 2017 · 2 comments
Open

draw_image always results in a 300x300 image #17

JasonBarnabe opened this issue Jun 2, 2017 · 2 comments

Comments

@JasonBarnabe
Copy link

@instructions << PDF::Instruction.new('cm', 300, 0, 0, 300, x, y)

This should allow passing in width and height parameters.

@gdelugre
Copy link
Owner

Actually those are just dummy values in the transformation matrix, they do not mean anything.

The support for drawing images in Origami is very limited. I don't think this is going to work with this code, there is no support of colorimetric spaces, image attributes and so on. I should probably just have raised a NotImplementedError.

@JasonBarnabe
Copy link
Author

I'm adding images to PDFs with no problems other than this one. Those 300s really do represent the image's dimensions.

require 'origami'
include Origami

def add_kitty(cm_with_size)

  pdf = PDF.read('pdf-sample.pdf')
  page = pdf.get_page(1)

  img = Graphics::ImageXObject.from_image_file('1327.jpeg', 'jpg')
  img.Width  = 500
  img.Height = 422
  img.ColorSpace = Origami::Graphics::Color::Space::DEVICE_RGB
  img.BitsPerComponent = 8
  img.Interpolate = false
  page.add_xobject(img, :kitty)

  contents = Origami::ContentStream.new

  # Reimplementation of ContentStream#draw_image
  contents.instructions << PDF::Instruction.new('q')
  if cm_with_size
    contents.instructions << PDF::Instruction.new('cm', img.Width, 0, 0, img.Height, 0, 0)
  else
    contents.instructions << PDF::Instruction.new('cm', 300, 0, 0, 300, 0, 0)
  end
  contents.instructions << PDF::Instruction.new('Do', :kitty)
  contents.instructions << PDF::Instruction.new('Q')

  page.setContents([page.Contents, contents])

  pdf.save("pdf-with-kitty-#{cm_with_size ? 'cm_with_size' : 'cm_default'}.pdf")
end

add_kitty(true)
add_kitty(false)

Inputs:

1327
pdf-sample.pdf

Outputs:

pdf-with-kitty-cm_default.pdf
pdf-with-kitty-cm_with_size.pdf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants