私はJavascriptアプリを持っています。ボタンをクリックすると、POST 要求を使用して JSON オブジェクトが Django-Pisa リモート サーバーに送信され、JSON オブジェクトから PDF ファイルが作成されます。JSON の長さが GET の許容長をはるかに超えているため、POST を使用する必要があります。
これは私の Django レンダリング関数です
@csrf_exempt
def render_to_pdf(request):
request_data = ast.literal_eval(request.POST.keys()[0])
template_src = templates_map.TEMPLATES_MAP[request_data['intervention']]
context_json = request_data['data']
template = get_template(template_src)
context = Context(context_json)
html = template.render(context)
result = StringIO.StringIO()
pdf = pisa.pisaDocument(StringIO.StringIO(html), result,link_callback=fetch_resources)
if not pdf.err:
return HttpResponse(result.getvalue(), content_type='application/pdf')
return HttpResponse('We had some errors<pre>%s</pre>' % escape(html))
JavaScript側では、これはクリックイベントです
try {
jQuery.post('http://pdfgen-server/pdfgen', JSON.stringify(requestData),
function(data) {
var w = window.open();
w.document.write(data);
});
}
catch (err) {
; //error handling
}
クリックすると、新しいウィンドウが表示され、レンダリングされた PDF ファイルではなく、文字通り PDF コンテンツ (メモ帳を開いて PDF ファイルを表示するようなもの) が表示されます。新しいブラウザ ウィンドウの最初の数行:
%PDF-1.4 % ReportLab 生成 PDF ドキュメントhttp://www.reportlab.com % 'BasicFonts': class PDFDictionary 1 0 obj % 標準フォント辞書 << /F1 2 0 R /F2 3 0 R /F3 4 0 R /F4 5 0 R >> endobj % 'F1': class PDFType1Font 2 0 obj % Font Helvetica << /BaseFont /Helvetica /Encoding /WinAnsiEncoding /Name /F1 /Subtype /Type1 /Type /Font >> endobj % 'F2' : class PDFType1Font 3 0 obj % Font Times-Roman << /BaseFont /Times-
この問題を解決する方法を教えてください。
ありがとう