Skip to content

Commit

Permalink
chore: warn if wkhtmltopdf is invalid (#26174)
Browse files Browse the repository at this point in the history
* chore: warn if wkhtmltopdf is invalid

wkhtmltopdf ( with patched qt ) is required to generate pdfs properly.
when user clicks on PDF, pdf will be generated and downloaded.
however, on print preview page warning will be shown.

* chore: refactor based on review comments

* chore: return False incase of exception

* chore: refactor and better naming

Co-authored-by: Ankush Menat <ankushmenat@gmail.com>
(cherry picked from commit 6a6ded1)
  • Loading branch information
maharshivpatel authored and mergify[bot] committed Apr 30, 2024
1 parent a1f008c commit 6621251
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
20 changes: 19 additions & 1 deletion frappe/printing/page/print/print.js
Expand Up @@ -603,7 +603,24 @@ frappe.ui.form.PrintView = class {
},
});
}

async is_wkhtmltopdf_valid() {
const is_valid = await frappe.xcall("frappe.utils.pdf.is_wkhtmltopdf_valid");
// function returns true or false
if (is_valid) return;
frappe.msgprint({
title: __("Invalid wkhtmltopdf version"),
message:
__("PDF generation may not work as expected.") +
"<hr/>" +
__("Please contact your system manager to install correct version.") +
"<br/>" +
__("Correct version :") +
" <b><a href ='https://wkhtmltopdf.org/downloads.html'>" +
__("wkhtmltopdf 0.12.x (with patched qt).") +
"</a></b>",
indicator: "red",
});
}
render_pdf() {
let print_format = this.get_print_format();
if (print_format.print_format_builder_beta) {
Expand All @@ -619,6 +636,7 @@ frappe.ui.form.PrintView = class {
return;
}
} else {
this.is_wkhtmltopdf_valid();
this.render_page("/api/method/frappe.utils.print_format.download_pdf?");
}
}
Expand Down
11 changes: 11 additions & 0 deletions frappe/utils/pdf.py
Expand Up @@ -18,6 +18,7 @@
from frappe import _
from frappe.core.doctype.file.utils import find_file_by_url
from frappe.utils import cstr, scrub_urls
from frappe.utils.caching import redis_cache
from frappe.utils.jinja_globals import bundled_asset, is_rtl

PDF_CONTENT_ERRORS = [
Expand Down Expand Up @@ -352,6 +353,16 @@ def toggle_visible_pdf(soup):
tag.extract()


@frappe.whitelist()
@redis_cache(ttl=60 * 60)
def is_wkhtmltopdf_valid():
try:
output = subprocess.check_output(["wkhtmltopdf", "--version"])
return "qt" in output.decode("utf-8").lower()
except Exception:
return False


def get_wkhtmltopdf_version():
wkhtmltopdf_version = frappe.cache.hget("wkhtmltopdf_version", None)

Expand Down

0 comments on commit 6621251

Please sign in to comment.