@@ -27,65 +27,68 @@ pdf_on_submit.build_pdf_url = function (frm, { print_format, letter_head }) {
2727 return frappe . urllib . get_full_url ( "/api/method/frappe.utils.print_format.download_pdf?" + params ) ;
2828} ;
2929
30- pdf_on_submit . add_pdf_button = async function ( frm ) {
31- if ( frm . is_new ( ) ) return ;
30+ pdf_on_submit . add_pdf_button = function ( frm ) {
31+ if ( frm . is_new ( ) || ! frm . has_perm ( "print" ) ) {
32+ return ;
33+ }
3234
33- frm . remove_custom_button ( __ ( "PDF" ) ) ;
34-
35- if ( ! frm . has_perm ( "print" ) ) return ;
36-
37- frm . add_custom_button ( __ ( "PDF" ) , async ( ) => {
38- // Open the popup synchronously while still in the user-gesture context,
39- // before any await, to avoid browser popup blockers.
40- const popup = window . open ( "" , "_blank" ) ;
41- if ( ! popup ) {
42- frappe . msgprint ( {
43- title : __ ( "Popup Blocked" ) ,
44- indicator : "orange" ,
45- message : __ ( "Please allow popups for this site and try again." ) ,
46- } ) ;
47- return ;
48- }
49- try {
50- const matches = await frappe . xcall ( "pdf_on_submit.utils.get_print_details" , {
51- doctype : frm . doc . doctype ,
52- docname : frm . doc . name ,
53- } ) ;
54-
55- if ( ! matches || ! matches . length ) {
56- popup . close ( ) ;
35+ frm . page . add_button (
36+ __ ( "PDF" ) ,
37+ async ( ) => {
38+ // Open the popup synchronously while still in the user-gesture context,
39+ // before any await, to avoid browser popup blockers.
40+ const popup = window . open ( "" , "_blank" ) ;
41+ if ( ! popup ) {
42+ frappe . msgprint ( {
43+ title : __ ( "Popup Blocked" ) ,
44+ indicator : "orange" ,
45+ message : __ ( "Please allow popups for this site and try again." ) ,
46+ } ) ;
5747 return ;
5848 }
49+ try {
50+ const matches = await frappe . xcall ( "pdf_on_submit.utils.get_print_details" , {
51+ doctype : frm . doc . doctype ,
52+ docname : frm . doc . name ,
53+ } ) ;
54+
55+ if ( ! matches || ! matches . length ) {
56+ popup . close ( ) ;
57+ return ;
58+ }
5959
60- const [ first , ...rest ] = matches ;
61- popup . location . href = pdf_on_submit . build_pdf_url ( frm , first ) ;
60+ const [ first , ...rest ] = matches ;
61+ popup . location . href = pdf_on_submit . build_pdf_url ( frm , first ) ;
6262
63- if ( rest . length ) {
64- const items = rest
65- . map ( ( m ) => {
66- const label = m . letter_head
67- ? `${ frappe . utils . escape_html ( m . print_format ) } – ${ frappe . utils . escape_html ( m . letter_head ) } `
68- : frappe . utils . escape_html ( m . print_format ) ;
69- const href = frappe . utils . escape_html ( pdf_on_submit . build_pdf_url ( frm , m ) ) ;
70- return `<li><a href="${ href } " target="_blank" rel="noopener">${ label } </a></li>` ;
71- } )
72- . join ( "" ) ;
63+ if ( rest . length ) {
64+ const items = rest
65+ . map ( ( m ) => {
66+ const label = m . letter_head
67+ ? `${ frappe . utils . escape_html ( m . print_format ) } – ${ frappe . utils . escape_html ( m . letter_head ) } `
68+ : frappe . utils . escape_html ( m . print_format ) ;
69+ const href = frappe . utils . escape_html ( pdf_on_submit . build_pdf_url ( frm , m ) ) ;
70+ return `<li><a href="${ href } " target="_blank" rel="noopener">${ label } </a></li>` ;
71+ } )
72+ . join ( "" ) ;
73+ frappe . msgprint ( {
74+ title : __ ( "Additional print formats" ) ,
75+ indicator : "blue" ,
76+ message :
77+ __ ( "More formats are configured for this document. Click to open:" ) + `<ul>${ items } </ul>` ,
78+ } ) ;
79+ }
80+ } catch ( error ) {
81+ popup . close ( ) ;
82+ console . error ( "PDF generation failed:" , error ) ;
7383 frappe . msgprint ( {
74- title : __ ( "Additional print formats" ) ,
75- indicator : "blue" ,
76- message : __ ( "More formats are configured for this document. Click to open:" ) + `<ul>${ items } </ul>` ,
84+ title : __ ( "Error" ) ,
85+ indicator : "red" ,
86+ message : __ ( "Failed to generate PDF. {0}" , [
87+ error . message || __ ( "Please check your permissions and try again." ) ,
88+ ] ) ,
7789 } ) ;
7890 }
79- } catch ( error ) {
80- popup . close ( ) ;
81- console . error ( "PDF generation failed:" , error ) ;
82- frappe . msgprint ( {
83- title : __ ( "Error" ) ,
84- indicator : "red" ,
85- message : __ ( "Failed to generate PDF. {0}" , [
86- error . message || __ ( "Please check your permissions and try again." ) ,
87- ] ) ,
88- } ) ;
89- }
90- } ) ;
91+ } ,
92+ { icon : "small-file" }
93+ ) ;
9194} ;
0 commit comments