The PDF generated from the ERPNext report looks unformatted. I tried formatting it using this code, but the letterhead doesn’t appear. I’ve tried various things but haven’t succeeded; does anyone have a solution?
(function () {
“use strict”;
/*
* Global Query Report PDF Styling
*
* Affects ONLY Query Report PDF generation.
* Does NOT affect Print Formats.
*/
if (!frappe.views || !frappe.views.QueryReport) {
return;
}
const QueryReport = frappe.views.QueryReport;
if (QueryReport.prototype.__pmtech_pdf_patched) {
return;
}
QueryReport.prototype.__pmtech_pdf_patched = true;
const original_pdf_report = QueryReport.prototype.pdf_report;
QueryReport.prototype.pdf_report = async function (print_settings) {
try {
/\* =========================================================
\* 1. Basic Report Settings
\* ========================================================= \*/
const base_url = frappe.urllib.get_base_url();
const print_css = frappe.boot.print_css;
/\*
\* Use the user's selected Print Settings.
\*
\* These values come from the standard ERPNext
\* Print Settings dialog:
\*
\* - With Letter Head
\* - Letter Head
\* - Orientation
\* - Pick Columns
\*/
print_settings = print_settings || {};
const landscape =
String(print_settings.orientation || "").toLowerCase() === "landscape";
const is_rtl = frappe.utils.is_rtl();
const direction = is_rtl ? "rtl" : "ltr";
const custom_format =
this.report_settings.html_format || null;
/\* =========================================================
\* 2. Columns
\* ========================================================= \*/
const columns =
this.get_columns_for_print(
print_settings,
custom_format
);
/\* =========================================================
\* 3. Report Data
\* ========================================================= \*/
const data = this.get_data_for_print();
const applied_filters =
this.get_filter_values();
const filters_html =
this.get_filters_html_for_print();
/\* =========================================================
\* 4. Standard Report Template
\* ========================================================= \*/
const template =
print_settings.columns || !custom_format
? "print_grid"
: custom_format;
/\* =========================================================
\* 5. Render Standard ERPNext Report HTML
\* ========================================================= \*/
let html =
frappe.render_template(
template,
{
title: \_\_(this.report_name),
subtitle: filters_html,
filters: applied_filters,
data: data,
original_data: this.data,
columns: columns,
report: this,
base_url: base_url,
print_css: print_css,
print_settings: print_settings,
landscape: landscape,
lang: frappe.boot.lang,
layout_direction: direction,
can_use_smaller_font:
this.report_doc.is_standard === "Yes" &&
custom_format
? 0
: 1
}
);
/\* =========================================================
\* 6. LETTER HEAD
\*
\* IMPORTANT:
\* Do NOT query Letter Head manually here.
\*
\* The selected Letter Head is already supplied through
\* the standard Print Settings.
\*
\* We use:
\*
\* print_settings.with_letterhead
\* print_settings.letter_head
\*
\* and the Letter Head data already available to Frappe.
\* ========================================================= \*/
let letter_head_html = "";
if (
cint(print_settings.with_letterhead) &&
print_settings.letter_head
) {
/\*
\* Standard Frappe report printing exposes the selected
\* Letter Head through the boot letter_heads object when
\* available.
\*/
const letter_heads =
frappe.boot && frappe.boot.letter_heads
? frappe.boot.letter_heads
: {};
const selected_letter_head =
letter_heads\[print_settings.letter_head\];
if (selected_letter_head) {
/\*
\* Letter Head can be stored as:
\*
\* 1. string
\* 2. object containing header/content
\*/
if (typeof selected_letter_head === "string") {
letter_head_html =
selected_letter_head;
} else {
letter_head_html =
selected_letter_head.header ||
selected_letter_head.content ||
"";
}
}
}
/\* =========================================================
\* 7. Add Letter Head
\* ========================================================= \*/
if (letter_head_html) {
html =
\`
<div id="header-html" >
<div class="print-heading">
{% if letter_head %}
<div class="letter-head">{{ letter_head }}</div>
{% endif %}
</div>
</div>
\` +
html;
}
/\* =========================================================
\* 8. PMTech CSS
\* ========================================================= \*/
const pmtech_css = \`
@page {
size: A4 ${landscape ? "landscape" : "portrait"};
margin:
${letter_head_html ? "8mm" : "12mm"}
10mm
14mm
10mm;
}
\* {
box-sizing: border-box;
}
html {
width: 100%;
direction: ${direction} !important;
}
body {
width: 100%;
margin: 0 !important;
padding: 0 !important;
font-family:
Arial,
Tahoma,
"DejaVu Sans",
sans-serif !important;
font-size: 9.5pt !important;
color: #222 !important;
background: #fff !important;
direction: ${direction} !important;
text-align:
${is_rtl ? "right" : "left"} !important;
}
/\*
\* -----------------------------------------------------
\* Letter Head
\* -----------------------------------------------------
\*/
.pmtech-letter-head {
width: 100% !important;
display: block !important;
direction: ltr !important;
text-align: left !important;
margin: 0 0 8px 0 !important;
padding: 0 !important;
page-break-inside: avoid !important;
}
.pmtech-letter-head img {
max-width: 100% !important;
height: auto !important;
}
/\*
\* -----------------------------------------------------
\* Report
\* -----------------------------------------------------
\*/
.print-format,
.report-wrapper,
.print-grid,
.datatable {
width: 100% !important;
color: #222 !important;
direction: ${direction} !important;
}
/\*
\* -----------------------------------------------------
\* Titles
\* -----------------------------------------------------
\*/
h1,
h2,
h3 {
color: #14532d !important;
font-weight: bold !important;
}
/\*
\* -----------------------------------------------------
\* Tables
\* -----------------------------------------------------
\*/
table {
width: 100% !important;
border-collapse: collapse !important;
border-spacing: 0 !important;
margin: 8px 0 !important;
direction: ${direction} !important;
}
table th,
table td {
border:
1px solid #aeb7b2 !important;
padding:
5px 6px !important;
vertical-align:
middle !important;
line-height:
1.35 !important;
}
/\*
\* Header
\*/
table thead th,
table th {
background:
#e8f3ed !important;
color:
#14532d !important;
font-weight:
bold !important;
border:
1px solid #7d988a !important;
text-align:
center !important;
}
/\*
\* Body
\*/
table tbody tr:nth-child(even) td {
background:
#f8faf9 !important;
}
table tbody tr:nth-child(odd) td {
background:
#ffffff !important;
}
/\*
\* Do not use browser hover styling in PDF
\*/
table tbody tr:hover td {
background:
inherit !important;
}
/\*
\* -----------------------------------------------------
\* RTL / LTR
\* -----------------------------------------------------
\*/
\[dir="rtl"\],
.rtl {
direction: rtl !important;
text-align: right !important;
}
\[dir="ltr"\],
.ltr {
direction: ltr !important;
text-align: left !important;
}
/\*
\* Force report rows themselves to respect direction
\*/
.dt-row {
direction:
${direction} !important;
page-break-inside:
avoid !important;
}
.dt-cell {
direction:
${direction} !important;
}
.dt-cell__content {
direction:
${direction} !important;
}
/\*
\* -----------------------------------------------------
\* Alignment
\* -----------------------------------------------------
\*/
.text-right,
.text-end,
td.text-right,
td.text-end,
th.text-right,
th.text-end {
text-align:
right !important;
}
.text-center,
td.text-center,
th.text-center {
text-align:
center !important;
}
.text-left,
td.text-left,
th.text-left {
text-align:
left !important;
}
/\*
\* -----------------------------------------------------
\* Filters
\* -----------------------------------------------------
\*/
.filter-row {
display:
inline-block !important;
margin:
2px 12px 3px 0 !important;
padding:
3px 7px !important;
border:
1px solid #d5ddd8 !important;
border-radius:
3px !important;
background:
#f7faf8 !important;
}
.filter-row b {
color:
#14532d !important;
}
/\*
\* -----------------------------------------------------
\* Report Title
\* -----------------------------------------------------
\*/
.report-title,
.report-title h1,
.report-title h2 {
color:
#14532d !important;
font-weight:
bold !important;
}
/\*
\* -----------------------------------------------------
\* Total Rows
\* -----------------------------------------------------
\*/
tr.total-row td,
tr.total td,
.total-row td {
background:
#e8f3ed !important;
color:
#14532d !important;
font-weight:
bold !important;
border-top:
2px solid #14532d !important;
}
/\*
\* -----------------------------------------------------
\* Page Breaking
\* -----------------------------------------------------
\*/
tr {
page-break-inside:
avoid !important;
}
thead {
display:
table-header-group !important;
}
tfoot {
display:
table-footer-group !important;
}
.dt-scrollable {
overflow:
visible !important;
}
/\*
\* -----------------------------------------------------
\* Images
\* -----------------------------------------------------
\*/
img {
max-width:
100% !important;
}
/\*
\* -----------------------------------------------------
\* Links
\* -----------------------------------------------------
\*/
a {
color:
inherit !important;
text-decoration:
none !important;
}
/\*
\* -----------------------------------------------------
\* HR
\* -----------------------------------------------------
\*/
hr {
border:
0 !important;
border-top:
1px solid #aeb7b2 !important;
margin:
8px 0 !important;
}
/\*
\* -----------------------------------------------------
\* Page Break
\* -----------------------------------------------------
\*/
.page-break {
page-break-before:
always !important;
}
.print-format {
margin:
0 !important;
padding:
0 !important;
}
\`;
/\* =========================================================
\* 9. Add PMTech CSS
\* ========================================================= \*/
html =
"<style id='pmtech-global-report-pdf-css'>" +
pmtech_css +
"</style>" +
html;
/\* =========================================================
\* 10. PDF File Name
\* ========================================================= \*/
let filter_values = \[\];
let name_len = 0;
for (
const key of Object.keys(applied_filters)
) {
if (
applied_filters\[key\] == null
) {
continue;
}
name_len +=
applied_filters\[key\]
.toString()
.length;
if (name_len > 200) {
break;
}
filter_values.push(
applied_filters\[key\]
);
}
if (filter_values.length) {
print_settings.report_name =
\`${\_\_(this.report_name)}\_${filter_values.join("\_")}.pdf\`;
} else {
print_settings.report_name =
\`${\_\_(this.report_name)}.pdf\`;
}
/\* =========================================================
\* 11. Progress
\* ========================================================= \*/
frappe.show_progress(
\_\_("Preparing PDF"),
90,
100,
\_\_("Applying PMTech report styling...")
);
/\* =========================================================
\* 12. Generate PDF
\* ========================================================= \*/
frappe.render_pdf(
html,
print_settings
);
setTimeout(() => {
frappe.hide_progress();
}, 1500);
} catch (error) {
console.error(
"PMTech Global Report PDF Error:",
error
);
frappe.hide_progress();
/\*
\* If anything fails,
\* return to standard ERPNext PDF generation.
\*/
original_pdf_report.call(
this,
print_settings
);
}
};
})();