gettalong / hexapdf

Versatile PDF creation and manipulation for Ruby
https://hexapdf.gettalong.org
Other
1.25k stars 70 forks source link

HexaPDF::Error - Validation error for (17,0): Type of field BG is invalid: Symbol: #305

Closed zeeshan-nimonik closed 6 months ago

zeeshan-nimonik commented 6 months ago

I am encountering an issue with processing some PDFs using my code. HexaPDF::Error - Validation error for (17,0): Type of field BG is invalid: Symbol:

Below is the code snippet I am using:

FONT_HELVETICA = 'Helvetica'
FONT_SIZE = 6
FONT_COLOR_RED = 1.0
FONT_COLOR_GREEN = 0.0
FONT_COLOR_BLUE = 0.0

POSITION_X = 10
POSITION_Y = 20
OFFSET_Y = 7
input_pdf = Rails.public_path.join("ee.pdf")
output_pdf = Rails.public_path.join("output.pdf")
content = HexaPDF::Document.open(input_pdf)
font = content.fonts.add(FONT_HELVETICA)
content.pages.each do |page|
  watermark_text_rows.each_with_index do |text, index|
    canvas = page.canvas(type: :overlay)
    canvas.font(font, size: FONT_SIZE)
    canvas.fill_color(FONT_COLOR_RED, FONT_COLOR_GREEN, FONT_COLOR_BLUE)
    canvas.text(text, at: [POSITION_X, POSITION_Y - index * OFFSET_Y])
  end
end
File.open(output_pdf, 'wb') do |file|
  content.write(file)
end

The issue arises when the code reaches the content.write(file) line, and it throws an error.

Could you please review the code and help me resolve this issue? Here is the sample input.pdf

Thank you for your assistance.

gettalong commented 6 months ago

The file itself is not valid. If you run hexapdf info --check input.pdf it will tell you that there are problems with the file. The field /BG should only have dictionaries or streams as values but in your case it has a symbol as value. You can inspect the value using hexapdf inspect input.pdf 17.

You can turn validation off when writing using the validate argument. However, if you do that you also might wanna run the validation yourself using content.validate which will do the validation, auto-correct problems if possible but not throw any errors.