Skip to content

Commit

Permalink
Merge pull request crabbly#662 from laercioacj/master
Browse files Browse the repository at this point in the history
Add Footer
  • Loading branch information
crabbly authored Feb 26, 2023
2 parents ee73843 + efce09d commit 08d002d
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ declare namespace printJS {
documentTitle?: string;
header?: any;
headerStyle?: string;
footer?: any;
footerStyle?: string;
maxWidth?: number;
targetStyle?: string | string[];
targetStyles?: string | string[];
Expand Down
23 changes: 23 additions & 0 deletions src/js/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,29 @@ export function addHeader (printElement, params) {
printElement.insertBefore(headerContainer, printElement.childNodes[0])
}

export function addFooter (printElement, params) {
// Create the footer container div
let footerContainer = document.createElement('div')

// Check if the footer is text or raw html
if (isRawHTML(params.footer)) {
footerContainer.innerHTML = params.footer
} else {
// Create footer element
let footerElement = document.createElement('h1')

// Create footer text node
let footerNode = document.createTextNode(params.footer)

// Build and style
footerElement.appendChild(footerNode)
footerElement.setAttribute('style', params.footerStyle)
footerContainer.appendChild(footerElement)
}

printElement.insertBefore(footerContainer, printElement.childNodes.lastChild)
}

export function cleanUp (params) {
// If we are showing a feedback message to user, remove it
if (params.showModal) Modal.close()
Expand Down
7 changes: 6 additions & 1 deletion src/js/html.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { collectStyles, addHeader } from './functions'
import { collectStyles, addHeader, addFooter } from './functions'
import Print from './print'

export default {
Expand All @@ -20,6 +20,11 @@ export default {
addHeader(params.printableElement, params)
}

// Add footer
if (params.footer) {
addFooter(params.printableElement, params)
}

// Print html element contents
Print.send(params, printFrame)
}
Expand Down
7 changes: 6 additions & 1 deletion src/js/image.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addHeader } from './functions'
import { addHeader, addFooter } from './functions'
import Print from './print'
import Browser from './browser'

Expand Down Expand Up @@ -42,6 +42,11 @@ export default {
// Check if we are adding a print header
if (params.header) addHeader(params.printableElement, params)

// Check if we are adding a print footer
if (params.footer) {
addFooter(params.printableElement, params)
}

// Print image
Print.send(params, printFrame)
}
Expand Down
2 changes: 2 additions & 0 deletions src/js/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export default {
type: 'pdf',
header: null,
headerStyle: 'font-weight: 300;',
footer: null,
footerStyle: 'font-weight: 300;',
maxWidth: 800,
properties: null,
gridHeaderStyle: 'font-weight: bold; padding: 5px; border: 1px solid #dddddd;',
Expand Down
7 changes: 6 additions & 1 deletion src/js/json.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { capitalizePrint, addHeader } from './functions'
import { capitalizePrint, addHeader, addFooter } from './functions'
import Print from './print'

export default {
Expand Down Expand Up @@ -38,6 +38,11 @@ export default {
// Build the printable html data
params.printableElement.innerHTML += jsonToHTML(params)

// Check if we are adding a print footer
if (params.footer) {
addFooter(params.printableElement, params)
}

// Print the json data
Print.send(params, printFrame)
}
Expand Down
11 changes: 11 additions & 0 deletions src/js/raw-html.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Print from './print'
import { addHeader, addFooter } from './functions'

export default {
print: (params, printFrame) => {
Expand All @@ -9,6 +10,16 @@ export default {
// Set our raw html as the printable element inner html content
params.printableElement.innerHTML = params.printable

// Add header
if (params.header) {
addHeader(params.printableElement, params)
}

// Add footer
if (params.footer) {
addFooter(params.printableElement, params)
}

// Print html contents
Print.send(params, printFrame)
}
Expand Down
11 changes: 9 additions & 2 deletions test/manual/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
function printHtml() {
printJS({
printable: 'test',
header: 'JSON Print Test',
footer: 'JSON Print Footer',
type: 'html'
})
}
Expand All @@ -58,6 +60,8 @@
<p class="blueText">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>`,
type: 'raw-html',
header: 'JSON Print Header',
footer: 'JSON Print Footer',
style: '.blueText {color:blue;}'
})
}
Expand Down Expand Up @@ -106,7 +110,8 @@
}
],
type: 'json',
header: 'JSON Print Test',
header: 'JSON Print Test',
footer: 'JSON Print Footer',
documentTitle: 'Print.js JSON Test'
})
}
Expand Down Expand Up @@ -181,6 +186,8 @@
'test-01.jpg',
'test-02.jpg'
],
header: 'JSON Print Test',
footer: 'JSON Print Footer',
type: 'image'
})
}
Expand Down Expand Up @@ -317,4 +324,4 @@ <h2>Form Elements</h2>
</div>
</section>
</body>
</html>
</html>

0 comments on commit 08d002d

Please sign in to comment.