Skip to content

Instantly share code, notes, and snippets.

@chgad
Created August 19, 2019 11:08
Show Gist options
  • Save chgad/37f6783a3a242dcdfb1522847c236cf6 to your computer and use it in GitHub Desktop.
Save chgad/37f6783a3a242dcdfb1522847c236cf6 to your computer and use it in GitHub Desktop.
______________ TestReportApiEndpoint.test_compile_pdf_returns_pdf ______________
self = <test_views.TestReportApiEndpoint object at 0x7f0fba435320>
prepared_ReportViewSet_view = <api.views.ReportViewSet object at 0x7f0fba987ac8>
report_object = <Report: Report object (27b26abc-98dd-46a2-b49c-57c7b7fec82f)>
@pytest.mark.django_db
def test_compile_pdf_returns_pdf(self, prepared_ReportViewSet_view, report_object):
"""
Test whether the method actually returns a PDF file.
:param prepared_ReportViewSet_view:
:return:
"""
export_content = prepared_ReportViewSet_view.aggregate_export_content(
report_object
)
pdf = prepared_ReportViewSet_view.compile_pdf(
template_name="api/stundenzettel.html",
content_dict=export_content,
pdf_options={
"page-size": "Letter",
"margin-top": "30px",
"margin-right": "5px",
"margin-bottom": "5px",
"margin-left": "15px",
"encoding": "UTF-8",
> "no-outline": None,
},
)
api/tests/test_views.py:652:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
api/views.py:167: in compile_pdf
pdf = pdf_from_string(html, False, options=pdf_options)
../../../virtualenv/python3.6.7/lib/python3.6/site-packages/pdfkit/api.py:72: in from_string
return r.to_pdf(output_path)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <pdfkit.pdfkit.PDFKit object at 0x7f0fbab63dd8>, path = False
def to_pdf(self, path=None):
args = self.command(path)
result = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
# If the source is a string then we will pipe it into wkhtmltopdf.
# If we want to add custom CSS to file then we read input file to
# string and prepend css to it and then pass it to stdin.
# This is a workaround for a bug in wkhtmltopdf (look closely in README)
if self.source.isString() or (self.source.isFile() and self.css):
input = self.source.to_s().encode('utf-8')
elif self.source.isFileObj():
input = self.source.source.read().encode('utf-8')
else:
input = None
stdout, stderr = result.communicate(input=input)
stderr = stderr or stdout
try:
stderr = stderr.decode('utf-8')
except UnicodeDecodeError:
stderr = ''
exit_code = result.returncode
if 'cannot connect to X server' in stderr:
raise IOError('%s\n'
'You will need to run wkhtmltopdf within a "virtual" X server.\n'
'Go to the link below for more information\n'
'https://github.com/JazzCore/python-pdfkit/wiki/Using-wkhtmltopdf-without-X-server' % stderr)
if 'Error' in stderr:
> raise IOError('wkhtmltopdf reported an error:\n' + stderr)
E OSError: wkhtmltopdf reported an error:
E Loading pages (1/6)
E [> ] 0%
[======> ] 10%
[=============> ] 22%
[============================================================] 100%
Counting pages (2/6)
E [============================================================] Object 1 of 1
Resolving links (4/6)
E [============================================================] Object 1 of 1
Loading headers and footers (5/6)
E Printing pages (6/6)
E [> ] Preparing
[============================================================] Page 1 of 1
Done
E Exit with code 1 due to network error: ContentNotFoundError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment