2016-10-04 13 views
0

Mathjaxを含むHTMLファイルをpdfに変換して(印刷するために)どのように変換することができるか知りたいと思います。reportlab djangoを使用してmathjaxを含むhtmlファイルをpdfに変換する

VIEWS.PY をeasy_pdf.views輸入PDFTemplateView

クラスHelloPDFView(PDFTemplateView)から: テンプレート名= "mytemplate.html"

def get_context_data(self, **kwargs):  
    context = super(HelloPDFView, self).get_context_data(
      pagesize="A4", 
      title="Hi Pierre!", 
      **kwargs 
     ) 
    context.update({'variable':12}) 
    return context 

MyTemplateに私はそれがこのように動作させることを試みました。 HTML

{% extends "easy_pdf/base.html" %} 

{% block extra_style %} 
<script type="text/x-mathjax-config"> 
    MathJax.Hub.Config({ 
    tex2jax: {inlineMath: [["$","$"],["\\(","\\)"]]} 
    }); 
</script> 

<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_HTMLorMML"></script> 

{% endblock %} 

{% block content %} 
    <div id="content"> 
     <h1>Hi there!{{variable}}</h1> 

     {% autoescape on %} 
      $$ \frac{a}{b} x=2 $$ 
     {% endautoescape %} 
    </div> 
{% endblock %} 

URLS.PY

urlpatterns = patterns('', 
         ('^monpremierpdf.pdf$', HelloPDFView.as_view()), 
        ) 

しかし、私が期待していた結果は得られません。そして私はまだ私のpdfの中に "$$"を見ることができます。

誰かが私にこれを行う最も簡単な方法を説明することができれば、それは私が働いているプロジェクトのために本当に役立つだろう...

ありがとう!

+0

最終

import pdfcrowd def generate_pdf_view(request): path_to_html_file = os.path.join(settings.PROJECT_ROOT,"templates/mytemplate.html") try: # create an API client instance client = pdfcrowd.Client("<username>", "<API_key>") # convert a web page and store the generated PDF to a variable pdf = client.convertFile(path_to_html_file) # set HTTP response headers response = HttpResponse(content_type="application/pdf") response["Cache-Control"] = "max-age=0" response["Accept-Ranges"] = "none" response["Content-Disposition"] = "attachment; filename=myamazingPDF.pdf" # send the generated PDF response.write(pdf) except pdfcrowd.Error, why: response = HttpResponse(content_type="text/plain") response.write(why) return response 

そして、私が望むように、結果が正確では...、ReportLabのは、JavaScriptを実行しませんでした。数学的な断片や完全なhtmlを傍受して、それがreportlabsに渡される前に、mathjax-nodeを使って何かできることがあります。 –

+0

将来の注意:cdn.mathjax.orgの寿命が近づいているので、移行のヒントについてはhttps://www.mathjax.org/cdn-shutting-down/をチェックしてください。 –

答えて

0

最終的に解決策が見つかりました:使用pdfcrowd

1°。ここに購読する:pdfcrowd.com 2°。彼らはあなたにユーザー名とAPIキーを提供します(最初の100トークンは無料です) 3°。実行:pip install pdfcrowd 4°。 MathJaxは実行されませんので、私がチェック

関連する問題