14.3.123. crate_anon.crateweb.extra.pdf

crate_anon/crateweb/extra/pdf.py


Copyright (C) 2015, University of Cambridge, Department of Psychiatry. Created by Rudolf Cardinal (rnc1001@cam.ac.uk).

This file is part of CRATE.

CRATE is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

CRATE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with CRATE. If not, see <https://www.gnu.org/licenses/>.


Assistance functions for working with PDFs.

class crate_anon.crateweb.extra.pdf.CratePdfPlan(*args, ethics_doccode: str | None = None, **kwargs)[source]

Specializes cardinal_pythonlib.pdf.PdfPlan for our default header/footer.

__init__(*args, ethics_doccode: str | None = None, **kwargs) None[source]
Parameters:
  • is_html – use HTML mode?

  • html – for HTML mode, the main HTML

  • header_html – for HTML mode, an optional page header (in HTML)

  • footer_html – for HTML mode, an optional page footer (in HTML)

  • wkhtmltopdf_filename – filename of the wkhtmltopdf executable

  • wkhtmltopdf_options – options for wkhtmltopdf

  • is_filename – use file mode?

  • filename – for file mode, the filename of the existing PDF on disk

Use either is_html or is_filename, not both.

Returns footer HTML for PDF creation via wkhtmltopdf. Replaces settings.PDF_LETTER_FOOTER_HTML.

crate_anon.crateweb.extra.pdf.get_pdf_from_html_with_django_settings(html: str, header_html: str | None = None, footer_html: str | None = None, wkhtmltopdf_filename: str | None = None, wkhtmltopdf_options: Dict[str, Any] | None = None, debug_content: bool = False, debug_options: bool = False, fix_pdfkit_encoding_bug: bool | None = None) bytes[source]

Applies our settings.WKHTMLTOPDF_OPTIONS and then makes a PDF from the supplied HTML.

See the arguments to cardinal_pythonlib.pdf.make_pdf_from_html().

Returns:

a binary PDF

crate_anon.crateweb.extra.pdf.get_pdf_header_html() str[source]

Returns header HTML for PDF creation via wkhtmltopdf. Replaces settings.PDF_LETTER_HEADER_HTML.

crate_anon.crateweb.extra.pdf.make_pdf_on_disk_from_html_with_django_settings(html: str, header_html: str | None = None, footer_html: str | None = None, wkhtmltopdf_filename: str | None = None, wkhtmltopdf_options: Dict[str, Any] | None = None, output_path: str | None = None, debug_content: bool = False, debug_options: bool = False, fix_pdfkit_encoding_bug: bool | None = None) bool[source]

Applies our settings.WKHTMLTOPDF_OPTIONS and then makes a PDF from the supplied html and stores it in the file named by output_path.

See the arguments to cardinal_pythonlib.pdf.make_pdf_from_html().

Returns:

success?

crate_anon.crateweb.extra.pdf.serve_html_or_pdf(html: str, viewtype: str, ethics_doccode: str | None = None) HttpResponse[source]

Serves some HTML as HTML or after converting it to a PDF in our letter style. For development.

Parameters:
  • html – contents

  • viewtype"pdf" or "html"

  • ethics_doccode – ethics document code

crate_anon.crateweb.extra.pdf.serve_pdf_from_html(html: str, offered_filename: str = 'test.pdf', **kwargs) HttpResponse[source]

Converts HTML into a PDF and serves it.

Parameters: