0.5.14 pdf articles export
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import json
|
||||
import os
|
||||
|
||||
from django.http import HttpResponse, Http404, FileResponse, HttpResponseRedirect
|
||||
from django.template import loader, RequestContext
|
||||
@@ -10,7 +11,7 @@ from django.utils.translation import gettext_lazy as _
|
||||
from django.urls import reverse
|
||||
from collections import OrderedDict
|
||||
from django.template.loader import render_to_string
|
||||
|
||||
from urllib.parse import unquote
|
||||
|
||||
def get_tree_arts(art, arts, sel_arts_ids):
|
||||
|
||||
@@ -28,15 +29,7 @@ def get_tree_arts(art, arts, sel_arts_ids):
|
||||
return docs_tree
|
||||
|
||||
|
||||
import os
|
||||
def link_callback(uri, rel):
|
||||
if settings.STATIC_URL and uri.startswith(settings.STATIC_URL):
|
||||
path = os.path.join(settings.STATIC_ROOT, uri.replace(settings.STATIC_URL, ""))
|
||||
elif settings.MEDIA_URL and uri.startswith(settings.MEDIA_URL):
|
||||
path = os.path.join(settings.MEDIA_ROOT, uri.replace(settings.MEDIA_URL, ""))
|
||||
else:
|
||||
path = os.path.join(settings.STATIC_ROOT, uri)
|
||||
return path
|
||||
|
||||
|
||||
|
||||
def _response_pdf(path):
|
||||
@@ -54,17 +47,40 @@ def _response_pdf(path):
|
||||
|
||||
def pdf_render(request, template, context, file_name):
|
||||
from BaseModels.functions import url_translit
|
||||
from GeneralApp.funcs_options import get_options_by_opt_types
|
||||
|
||||
filename = url_translit(file_name)
|
||||
|
||||
html = render_to_string(template, context, request)
|
||||
|
||||
sets = get_options_by_opt_types(['domain'], only_vals=True, w_prefix=True)
|
||||
if settings.DEBUG:
|
||||
sets['domain'] = 'http://127.0.0.1:8019'
|
||||
while 'src="/' in html:
|
||||
html = html.replace('src="/', f'src="{sets["domain"]}/')
|
||||
# html = unquote(html)
|
||||
|
||||
|
||||
f = open(f'{settings.PDF_FOLDER}/{filename}.html', 'w')
|
||||
f.write(html)
|
||||
f.close()
|
||||
|
||||
os.system(
|
||||
u'{wkhtmltopdf} --load-error-handling ignore --default-header {pdf_folder}/{file_name}.html {pdf_folder}/{file_name}.pdf'.format(
|
||||
wkhtmltopdf=settings.WKHTML_TO_PDF, pdf_folder=settings.PDF_FOLDER, file_name=filename))
|
||||
'{wkhtmltopdf} '
|
||||
# '--margin-top 20mm '
|
||||
# '--encoding windows-1250 '
|
||||
'--enable-local-file-access '
|
||||
'--load-error-handling ignore '
|
||||
'--enable-internal-links '
|
||||
'--keep-relative-links '
|
||||
'--print-media-type '
|
||||
'--encoding UTF-8 '
|
||||
# '--default-header '
|
||||
# '--minimum-font-size 20mm '
|
||||
'{pdf_folder}/{file_name}.html '
|
||||
'{pdf_folder}/{file_name}.pdf'.format(
|
||||
wkhtmltopdf=settings.WKHTML_TO_PDF, pdf_folder=settings.PDF_FOLDER, file_name=filename)
|
||||
)
|
||||
path = settings.PDF_FOLDER + '/' + filename + '.pdf'
|
||||
# os.system(u'{wkhtmltopdf} {print_folder}/order{filename}.html {print_folder}/order{filename}.pdf'.format(wkhtmltopdf=settings.WKHTML_TO_PDF, print_folder=settings.PRINT_FOLDER))
|
||||
return _response_pdf(path)
|
||||
|
||||
Reference in New Issue
Block a user