@@ -10,7 +10,13 @@ $(document).on("app_ready", function () {
1010
1111 ( boot . enabled_doctypes || [ ] ) . forEach ( ( doctype ) => {
1212 frappe . ui . form . on ( doctype , {
13- refresh : pdf_on_submit . add_pdf_button ,
13+ refresh : function ( frm ) {
14+ if ( frm . is_new ( ) || ! frm . has_perm ( "print" ) ) {
15+ return ;
16+ }
17+
18+ frm . page . add_button ( __ ( "PDF" ) , ( ) => pdf_on_submit . open_pdf ( frm ) , { icon : "small-file" } ) ;
19+ } ,
1420 } ) ;
1521 } ) ;
1622} ) ;
@@ -27,68 +33,57 @@ pdf_on_submit.build_pdf_url = function (frm, { print_format, letter_head }) {
2733 return frappe . urllib . get_full_url ( "/api/method/frappe.utils.print_format.download_pdf?" + params ) ;
2834} ;
2935
30- pdf_on_submit . add_pdf_button = function ( frm ) {
31- if ( frm . is_new ( ) || ! frm . has_perm ( "print" ) ) {
36+ pdf_on_submit . open_pdf = async function ( frm ) {
37+ // Open the popup synchronously while still in the user-gesture context,
38+ // before any await, to avoid browser popup blockers.
39+ const popup = window . open ( "" , "_blank" ) ;
40+ if ( ! popup ) {
41+ frappe . msgprint ( {
42+ title : __ ( "Popup Blocked" ) ,
43+ indicator : "orange" ,
44+ message : __ ( "Please allow popups for this site and try again." ) ,
45+ } ) ;
3246 return ;
3347 }
48+ try {
49+ const matches = await frappe . xcall ( "pdf_on_submit.utils.get_print_details" , {
50+ doctype : frm . doc . doctype ,
51+ docname : frm . doc . name ,
52+ } ) ;
3453
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- } ) ;
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+ if ( ! matches || ! matches . length ) {
55+ popup . close ( ) ;
56+ return ;
57+ }
5458
55- if ( ! matches || ! matches . length ) {
56- popup . close ( ) ;
57- return ;
58- }
59-
60- const [ first , ...rest ] = matches ;
61- popup . location . href = pdf_on_submit . build_pdf_url ( frm , first ) ;
59+ const [ first , ...rest ] = matches ;
60+ popup . location . href = pdf_on_submit . build_pdf_url ( frm , first ) ;
6261
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 ) ;
83- frappe . msgprint ( {
84- title : __ ( "Error" ) ,
85- indicator : "red" ,
86- message : __ ( "Failed to generate PDF. {0}" , [
87- error . message || __ ( "Please check your permissions and try again." ) ,
88- ] ) ,
89- } ) ;
90- }
91- } ,
92- { icon : "small-file" }
93- ) ;
62+ if ( rest . length ) {
63+ const items = rest
64+ . map ( ( m ) => {
65+ const label = m . letter_head
66+ ? `${ frappe . utils . escape_html ( m . print_format ) } – ${ frappe . utils . escape_html ( m . letter_head ) } `
67+ : frappe . utils . escape_html ( m . print_format ) ;
68+ const href = frappe . utils . escape_html ( pdf_on_submit . build_pdf_url ( frm , m ) ) ;
69+ return `<li><a href="${ href } " target="_blank" rel="noopener">${ label } </a></li>` ;
70+ } )
71+ . join ( "" ) ;
72+ frappe . msgprint ( {
73+ title : __ ( "Additional print formats" ) ,
74+ indicator : "blue" ,
75+ message : __ ( "More formats are configured for this document. Click to open:" ) + `<ul>${ items } </ul>` ,
76+ } ) ;
77+ }
78+ } catch ( error ) {
79+ popup . close ( ) ;
80+ console . error ( "PDF generation failed:" , error ) ;
81+ frappe . msgprint ( {
82+ title : __ ( "Error" ) ,
83+ indicator : "red" ,
84+ message : __ ( "Failed to generate PDF. {0}" , [
85+ error . message || __ ( "Please check your permissions and try again." ) ,
86+ ] ) ,
87+ } ) ;
88+ }
9489} ;
0 commit comments