Skip to content

Instantly share code, notes, and snippets.

@Uysim
Created December 9, 2016 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Uysim/12e56694410cb47d8d5c6ddbc958a170 to your computer and use it in GitHub Desktop.
Save Uysim/12e56694410cb47d8d5c6ddbc958a170 to your computer and use it in GitHub Desktop.
Background print is use for print page without open in tab. Its purpose use for print PDF.
# new BackgroundPrint('http://book.pdf').print()
class window.BackgroundPrint
constructor: (url)->
@url = url
print: ->
@_createFrame()
@_handleFrameLoad()
@_loadFrame()
_createFrame: ->
@iframe = document.createElement('iframe')
document.body.appendChild @iframe
@iframe.style.display = 'none'
_handleFrameLoad: ->
service = @
@iframe.onload = ->
setTimeout((->
service.iframe.focus()
service.iframe.contentWindow.print()
), 1)
_loadFrame: ->
@iframe.src = @url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment