как дать пользовательское поле для обложки с wicked_pdf в ruby ​​на рельсах - PullRequest
0 голосов
/ 23 сентября 2019

Мне не нужна маржа для первой страницы при генерации PDF

это код ruby ​​для генерации pdf

  def files
    respond_to do |format|
      format.html
      format.pdf do
        render pdf: params[:filename],
               page_size: 'A4',
               dpi: '300',
               user_style_sheet: '../assets/stylesheet/quotation.css',
               margin:  {
                top:               0,
                bottom:            0,
                left:              0,
                right:             0
              }
      end
    end
  end

1 Ответ

0 голосов
/ 23 сентября 2019

Верхние и нижние колонтитулы и поля являются глобальными для созданного PDF-документа, поэтому вы не можете настроить титульную страницу независимо.

Однако вы можете создать два PDF-файла, один из которых только обложка, и один изостальные и объедините их с Ghostscript или PDFtk.

html_content = render_to_string
cover_pdf = WickedPdf.new.pdf_from_string(html_content, { footer: { margin: { bottom: 200 })
body_pdf = WickedPdf.new.pdf_from_string(html_content, { footer: { margin: { bottom: 10 })

cover_src_temp_file = Tempfile.new(['cover_src', '.pdf'])
cover_src_temp_file.binmode
cover_src_temp_file.write(cover_pdf)
cover_src_temp_file.rewind
cover_temp_file = Tempfile.new(cover_pdf)
`pdftk #{cover_src_temp_file} cat 1 output #{cover_temp_file.path.to_s}` # first page only

body_src_temp_file = Tempfile.new(['body_src', '.pdf'])
body_src_temp_file.binmode
body_src_temp_file.write(cover_pdf)
body_src_temp_file.rewind
body_temp_file = Tempfile.new(body_pdf)
`pdftk #{body_src_temp_file.path} cat 2-end output #{body_temp_file.path}` # everything else

output_temp_file = Tempfile.new(['output', '.pdf'])
`pdftk #{cover_temp_file.path} #{body_temp_file.path} cat output #{output_temp_file.path}`
send_file output_temp_file, disposition: 'inline'

[cover_src_temp_file, body_src_temp_file, cover_temp_file, body_temp_file, output_temp_file].each do |tf|
tf.close
tf.unlink
end

Источник - https://github.com/mileszs/wicked_pdf/issues/572#issuecomment-245044036

...