Webkit2 export html to PDF

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Webkit2 export html to PDF

Ondrej Tuma-2
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.
--
Ondřej Tůma <[hidden email]>
www: http://ipv6.mcbig.cz   jabber: [hidden email]   twitter: mcbig_cz

_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
'-'
Reply | Threaded
Open this post in threaded view
|

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
> [hidden email]
> https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: Webkit2 export html to PDF

Ondrej Tuma-2
Great!

Thank's a lot, but it have one problem, which i must to resolve,
Name of printer is localized, so i must get this printer name from
system. But this could be the way :-)

Ondra

V Wed, 22 Nov 2017 01:36:05 +0200
'-' <[hidden email]> napsáno:

> 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
> > [hidden email]
> > https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list 


--
Ondřej Tůma <[hidden email]>
www: http://ipv6.mcbig.cz   jabber: [hidden email]   twitter: mcbig_cz

_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list