Re: Webkit2 export html to PDF



Hello,

You can configure WebKit2.PrintOperation with Gtk.PrintSettings, for example the following script (tested on gjs 1.50.1, Ubuntu 17.10) loads https://www.gtk.org/ and prints it to /tmp/output.pdf:

```js
#!/usr/bin/gjs
const { Application, ApplicationWindow, PrintSettings } = imports.gi.Gtk;
const { LoadEvent, WebView } = imports.gi.WebKit2;
const application = new Application();

application.connect("activate", () => {
  const win = new ApplicationWindow({ application });
  const webView = new WebView();
  win.add(webView);

  webView.connect("print", (_, printOperation) => {
    const printSettings = new PrintSettings();
    printSettings.set("output-file-format", "pdf");
    printSettings.set("output-uri", "file:///tmp/output.pdf");
    printSettings.set("printer", "Print to File");
    printOperation.set_print_settings(printSettings);

    printOperation.connect("finished", () => {
      win.destroy();
    });

    printOperation.print();

    const preventDefault = true;
    return preventDefault;
  });

  webView.connect("load_changed", (_, loadEvent) => {
    if (loadEvent === LoadEvent.FINISHED) {
      webView.run_javascript("print();", null, null);
    }
  });

  webView.load_uri("https://www.gtk.org/";);
});

application.run(ARGV);
```

On 2017-11-21 21:59, Ondrej Tuma wrote:
Hi there,

do you someone here know, ho to get pdf from html via WebKit2?

I generate html page from some another sources, and i want to export
pdf from this html. I can generate pdf by another library, but i use
WebKit2, GTK+, resp. cairo, so these libraries are ready to that.

I was study webkit2pdf tool, but it use WebKit1 which can print page
via GtkPrintOperation, but WebKit2 use it's WebKitPrintOperation which
don't gtk_print_operation_set_export_filename function :-(

Thank's for advice.
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]