Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python将json/html转换为pdf_Python_Html_Json_Pdf - Fatal编程技术网

Python将json/html转换为pdf

Python将json/html转换为pdf,python,html,json,pdf,Python,Html,Json,Pdf,我正在编写一个脚本,用于将带有html的json文件转换为呈现的pdf。 我写道: from weasyprint import HTML from django.template import Template, Context from django.template.loader import render_to_string, get_template import json context = json.load(open("source path")) rendered_s

我正在编写一个脚本,用于将带有html的json文件转换为呈现的pdf。 我写道:

from weasyprint import HTML  
from django.template import Template, Context
from django.template.loader import render_to_string, get_template
import json


context = json.load(open("source path"))

rendered_string = render_to_string("template.html", context)

HTML(string=rendered_string).write_pdf("sample.pdf")
问题是输出,json中的所有html都被格式化为简单文本和绑定类型。。。json部分还可以。 有人知道我该怎么解决这个问题

这是模板:

{%for elemento in elementi%}
 {%if not elemento.tipo%}
   <h1>{{elemento.html}}</h1>
 {%else%}
   <ul>
   {%for f in elemento.form %}
     <li>{{f}}</li>
   {%endfor%}
   </ul>
 {%endif%}
{%endfor%} 
这是json:

{"elementi":[{"id":107729,"titolo":null,"tipo":false,"form":null,"html":"<span lessico='Questa' idx=\"0\" testo=\"testo\" show-modal=\"setModal()\" tables=\"updateTables(input)\">Questa</span> <span lessico='&egrave;' idx=\"1\" testo=\"testo\" show-modal=\"setModal()\" tables=\"updateTables(input)\">&egrave;</span> <span lessico='una' idx=\"2\" testo=\"testo\" show-modal=\"setModal()\" tables=\"updateTables(input)\">una</span> <span lessico='domanda' idx=\"3\" testo=\"testo\" show-modal=\"setModal()\" tables=\"updateTables(input)\">domanda</span>...","traduci":[],"inputLessicale":[],"esercizioId":101624,"italiano":null,"testo_principale":"Questa &egrave; una domanda...","testo_principale_pre":"","testo_principale_post":"","risposta_1":"","risposta_2":"","risposta_3":"","risposta_4":"","distrattore_1":"","distrattore_2":"","distrattore_3":"","distrattore_4":"","parole_gia_ricostruite":"","continua":false,"focus":true,"ignora_lessico":false,"etichetta_1":null,"etichetta_2":null,"etichetta_3":null,"etichetta_4":null,"etichetta_5":null},
{"id":107730,"titolo":"Scegli la risposta corretta","tipo":"M","form":["a scelta multipla","con risposta aperta","di tipo trova","di associazione"],"html":null,"traduci":[],"inputLessicale":[],"esercizioId":101624,"italiano":null,"testo_principale":"a scelta multipla","testo_principale_pre":"","testo_principale_post":"","risposta_1":"","risposta_2":"","risposta_3":"","risposta_4":"","distrattore_1":"con risposta aperta","distrattore_2":"di associazione","distrattore_3":"di tipo trova","distrattore_4":"","parole_gia_ricostruite":"","continua":false,"focus":false,"ignora_lessico":false,"etichetta_1":null,"etichetta_2":null,"etichetta_3":null,"etichetta_4":null,"etichetta_5":null}],"ordine_in_verifica":null}

Django的一个安全特性是避免HTML注入,请参见类似线程的回答:

而不是:

{{ elemento.html }}
使用以下命令:

{{ elemento.html | safe }}
从Django文档:

将字符串标记为在输出之前不需要进一步的HTML转义。当“自动转义”处于禁用状态时,此过滤器无效


链接:

使用weasyprint询问html呈现问题可以使用他们自己的通信方式更快地解决。IRC和GitHubI我不确定问题是否是weasyprint,我想我在模板中做错了什么