StefanSalewski / gintro

High level GObject-Introspection based GTK3/GTK4 bindings for Nim language
MIT License
296 stars 20 forks source link

gtk4 example - showing a PDF a page #213

Open mantielero opened 1 year ago

mantielero commented 1 year ago

I struggled a bit in order to display a PDF page using GTK4 and poppler. So this is for the record, just in case it might serve others:

import gintro/[gtk4, gobject, gio, poppler, cairo] 

proc drawEvent(widget: DrawingArea; context: Context; w, h: int) =#:bool {.discardable.} = #, userData: gpointer) =
  var doc  = newDocumentFromFile("file:////home/user/use/your/path/helloworld.pdf", nil)  # Any URL should work
  var page = doc.getPage(0)
  page.render(context)

proc appActivate(app: Application) =
  let window = newApplicationWindow(app)
  window.title = "GTK4 & Nim"
  window.defaultSize = (800, 600)

  let da = newDrawingArea()
  window.setChild(da)  
  da.setHExpand
  da.setVExpand  

  da.setDrawFunc(drawEvent)

  window.present

proc main =
  let app = newApplication("org.gtk.example")
  connect(app, "activate", appActivate)
  discard run(app)

main()

@StefanSalewski, don't hesitate to include it in the example folder if you find it useful.

I open and close the issue.

mantielero commented 1 year ago

As I said, just for the record.

Cheers

StefanSalewski commented 1 year ago

Thanks for reporting. I think we should leave this issue open, as it may be useful for others. Indeed that is why I leave most issues open. I hope you will have some success with poppler. Note that you can always fine tune or expand the generated poppler.nim manually, I often do that for first tests, before I modify the generator script gen.nim.