2011-04-10 3 views
15

레일즈 3 앱에서 뷰를 PDF로 렌더링하려고 시도 할 때 PDFKit (wkhtmltopdf)을 사용하고 있습니다. 내가 예상대로 wkhtmltopdf이 Rails.root 작품 내에서 wkhtmltopdf public/404.html tmp/404.pdf 등이 오류의 원인 아니라는 것을 알고PDFKit (wkhtmltopdf) 깨진 파이프, OS X 및 레일 3

# Controller 
def show 
    respond_to do |format| 
    format.html { render } 
    format.pdf do 
     html = render_to_string(:layout => false , :action => "show.html.haml") 
     kit = PDFKit.new(html) 
     send_data(kit.to_pdf, :filename => "invoice.pdf", :type => 'application/pdf') 
     return # to avoid double render call 
    end 
    end 
end 

# Gemfile 
... 
gem 'pdfkit' 
gem 'wkhtmltopdf' 
... 

:

PDFKit 내 컨트롤러 show 액션에서 send_data(kit.to_pdf, :filename => "generated.pdf", :type => 'application/pdf')-Errno::EPIPE (Broken pipe) 가리키는 렌더링합니다.

같은 방식으로 미들웨어를 사용한 후에 example from jonathanspies.com을 사용하고 있습니다. 수동으로 명령을 실행

command failed: "~/.rvm/gems/[email protected]/bin/wkhtmltopdf" "--page-size" "Letter" "--margin-right" "0.75in" "--margin-top" "0.75in" "--margin-bottom" "0.75in" "--encoding" "UTF-8" "--margin-left" "0.75in" "--quiet" "-" "-" 

및 사용 화면이 쉽지 인 --quiet 옵션을보고, 표시됩니다

# config/application.rb 
require 'pdfkit' 
config.middleware.use PDFKit::Middleware 

신선한 레일 3 응용 프로그램을 그것을 시도 후, 나는 다음과 같은 오류가 발생합니다 그게 - 그걸 봐야한다고 볼 수

/lib/pdfkit/pdfkit.rb:35 변경 다음과 예상대로 작동합니다 (미들웨어도).

args << '--quit' 

그래서 다시 한 번 질문을 작성하여 도움을 요청하면서 문제를 해결했습니다. (항상 세부 사항을 지불해야합니다.) 맞춤법 오류 (항상 눈치 채지 않는 어리석은 실수)를 수정하는 pull request을 제출했습니다. 아무도 나를 어쨌든 게시하는 것을 바라지 않기를 바랍니다.

+0

게시 해 주셔서 감사합니다. –

+0

솔루션을 답변으로 게시하고 동의 할 수 있습니다. 그냥 SO 엔진을 행복하게 만드십시오. :) –

답변

6

quiet 인수를 quit으로 변경하는 것에 관해서.

이 변경은 매우 오래된 버전의 wkhtmltopdf 바이너리를 사용하는 wkhtmltopdf gem을 사용하는 경우에만 유효합니다. 내가 맞춤법 실수가 wkhtmltopdf 보석과 함께 제공되는 기존 바이너리에 존재

10:33:40 tmp > wkhtmltopdf --version 
Name: 
    wkhtmltopdf 0.9.9 

License: 
    Copyright (C) 2008,2009 Wkhtmltopdf Authors. 



    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. 
    This is free software: you are free to change and redistribute it. There is NO 
    WARRANTY, to the extent permitted by law. 

Authors: 
    Written by Jakob Truelsen. Patches by Mário Silva, Benoit Garret and Emmanuel 
    Bouthenot. 

10:33:50 tmp > wkhtmltopdf --help | grep quit 
10:34:02 tmp > wkhtmltopdf --help | grep quiet 
    -q, --quiet       Be less verbose 
10:34:07 tmp > 

설치 한 최신 바이너리와

10:32:15 wkhtml >  wkhtmltopdf --version 
wkhtmltopdf 0.8.3 using wkhtmltopdf patched qt 
Copyright (C) 2008,2009 Jakob Truelsen, 
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> 
This is free software: you are free to change and redistribute it. 
There is NO WARRANTY, to the extent permitted by law. 

Written by Jakob Truelsen 
Patches by Mário Silva and Emmanuel Bouthenot 

10:32:16 wkhtml >  wkhtmltopdf --help | grep quit 
    -q, --quit      Be less verbose. 
10:32:16 wkhtml >  wkhtmltopdf --help | grep quite 
10:32:19 wkhtml > 

wkhtmltopdf 보석으로

. wkhtmltopdf gem을 포함했는지 여부에 따라 초기화 또는 무언가를 기반으로하는 원숭이 패치 pdfkit을 제안합니다.

pdfkit이 실행중인 wkthtmltopdf 버전을 인식하고 조건부로 해당 인수를 전환 한 pull 요청을 수락합니다.

+0

@ ovatic이 게시 한 해결책에 대한 의견으로 게시하려고 한 oops –

2

/lib/pdfkit/pdfkit.rb:35을 다음과 같이 변경하면 모든 것이 예상대로 작동합니다 (미들웨어에서도 마찬가지 임).

args << '--quit' 
+1

축약 형'args << "-q"'는 모든 버전에서 작동하기 때문에 더 낫다 –