-
Notifications
You must be signed in to change notification settings - Fork 107
feat: add optional PDF Button for Sales and Purchase Doctypes #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
barredterra
merged 31 commits into
alyf-de:version-16
from
HenningWendtland:add_pdf_button
Jun 12, 2026
Merged
Changes from 27 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
0013c27
feat: add Optional PDF Button for Sales and Purchase Doctypes
HenningWendtland 109b32c
chore: update translation files
HenningWendtland 38cf49e
refactor: replace individual scripts by wildcard hook
HenningWendtland 73eaefc
refactor: remove Doctype check in Python to allow user to add button …
HenningWendtland 2c22a49
fix: remove unneeded check
HenningWendtland c594776
fix: handle case that letter_head field does not exist
HenningWendtland 150f65b
fix: improve type hint
HenningWendtland 41277f1
fix: remove duplicate pdf button hook
HenningWendtland 362e70f
refactor: display pdf_button for all pdf_on_submit doctypes
HenningWendtland 3bfb0fd
chore: add tests
HenningWendtland 5cfdc90
fix: return empty list as bootinfo if show_pdf_button is not checked
HenningWendtland 2a1b17b
fix: open popup synchronously to avoid browser popup blockers
HenningWendtland ca0247a
fix: improve button description
HenningWendtland 6e02693
fix: add error message for blocked popups
HenningWendtland 0f96d8a
chore: update translation files
HenningWendtland baefd94
chore: rebase onto version-16 and update translation files
HenningWendtland fbd7411
refactor: use common backend for attach and pdf button
HenningWendtland 6cb4d72
fix: use xcall instaed of frappe.call
HenningWendtland e60c81b
fix: remove email in .json
HenningWendtland dce6c67
fix: reinsert deleted comments in hooks.py
HenningWendtland 61e822c
fix: remove dead try/except
HenningWendtland 5bd02f8
fix: remove unneeded existence check
HenningWendtland f0b1979
Merge remote-tracking branch 'upstream/version-16' into add_pdf_button
barredterra 300254e
test: remove bad test
barredterra ee7fde5
style: format JS
barredterra 12d5a04
style: format python
barredterra 5095f3b
style: format python (2)
barredterra cfa5a6f
test: remove _call helpers
HenningWendtland 37d12ec
fix: add client side permission check
HenningWendtland 5d1de42
fix: cleanup guard clause, add file icon
barredterra 68883b6
refactor: clearer separation
barredterra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // Shared PDF button utility. | ||
| // Auto-registers for every doctype listed in PDF on Submit Settings > enabled_for | ||
| // when the global "Show PDF Button" toggle is on (loaded from boot info). | ||
|
|
||
| window.pdf_on_submit = window.pdf_on_submit || {}; | ||
|
|
||
| $(document).on("app_ready", function () { | ||
| const boot = frappe.boot.pdf_on_submit || {}; | ||
| if (!boot.show_pdf_button) return; | ||
|
|
||
| (boot.enabled_doctypes || []).forEach((doctype) => { | ||
| frappe.ui.form.on(doctype, { | ||
| refresh: pdf_on_submit.add_pdf_button, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| pdf_on_submit.build_pdf_url = function (frm, { print_format, letter_head }) { | ||
| const params = new URLSearchParams({ | ||
| doctype: frm.doc.doctype, | ||
| name: frm.doc.name, | ||
| format: print_format, | ||
| no_letterhead: 0, | ||
| letterhead: letter_head || "", | ||
| ...(frm.doc.language && { _lang: frm.doc.language }), | ||
| }).toString(); | ||
| return frappe.urllib.get_full_url("/api/method/frappe.utils.print_format.download_pdf?" + params); | ||
| }; | ||
|
|
||
| pdf_on_submit.add_pdf_button = async function (frm) { | ||
| if (frm.is_new()) return; | ||
|
|
||
| frm.remove_custom_button(__("PDF")); | ||
|
|
||
| frm.add_custom_button(__("PDF"), async () => { | ||
|
HenningWendtland marked this conversation as resolved.
Outdated
|
||
| // Open the popup synchronously while still in the user-gesture context, | ||
| // before any await, to avoid browser popup blockers. | ||
| const popup = window.open("", "_blank"); | ||
| if (!popup) { | ||
| frappe.msgprint({ | ||
| title: __("Popup Blocked"), | ||
| indicator: "orange", | ||
| message: __("Please allow popups for this site and try again."), | ||
| }); | ||
| return; | ||
| } | ||
| try { | ||
| const matches = await frappe.xcall("pdf_on_submit.utils.get_print_details", { | ||
| doctype: frm.doc.doctype, | ||
| docname: frm.doc.name, | ||
| }); | ||
|
|
||
| if (!matches || !matches.length) { | ||
| popup.close(); | ||
| return; | ||
| } | ||
|
|
||
| const [first, ...rest] = matches; | ||
| popup.location.href = pdf_on_submit.build_pdf_url(frm, first); | ||
|
|
||
| if (rest.length) { | ||
| const items = rest | ||
| .map((m) => { | ||
| const label = m.letter_head | ||
| ? `${frappe.utils.escape_html(m.print_format)} – ${frappe.utils.escape_html(m.letter_head)}` | ||
| : frappe.utils.escape_html(m.print_format); | ||
| const href = frappe.utils.escape_html(pdf_on_submit.build_pdf_url(frm, m)); | ||
| return `<li><a href="${href}" target="_blank" rel="noopener">${label}</a></li>`; | ||
| }) | ||
| .join(""); | ||
| frappe.msgprint({ | ||
| title: __("Additional print formats"), | ||
| indicator: "blue", | ||
| message: __("More formats are configured for this document. Click to open:") + `<ul>${items}</ul>`, | ||
| }); | ||
| } | ||
| } catch (error) { | ||
| popup.close(); | ||
| console.error("PDF generation failed:", error); | ||
| frappe.msgprint({ | ||
| title: __("Error"), | ||
| indicator: "red", | ||
| message: __("Failed to generate PDF. {0}", [ | ||
| error.message || __("Please check your permissions and try again."), | ||
| ]), | ||
| }); | ||
| } | ||
| }); | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.