adrg / go-wkhtmltopdf

Handcrafted Go bindings for wkhtmltopdf and high-level HTML to PDF conversion interface
https://pkg.go.dev/github.com/adrg/go-wkhtmltopdf
MIT License
249 stars 21 forks source link

how to disable-javascript config #31

Open zjkyz8 opened 1 year ago

zjkyz8 commented 1 year ago

Hi guys,

how can i pass disable-javascript to wkhtmltopdf? Currently, the converter will hang up at 10%, and someone told disable-javascript need to set, this is link, so i want try.

Thanks

adrg commented 1 year ago

Hi @zjkyz8,

You can disable Javascript by setting the EnableJavascript field of the object you are trying to convert to false. Here's a minimal example of that:

package main

import (
    "log"
    "os"

    pdf "github.com/adrg/go-wkhtmltopdf"
)

func main() {
    // Initialize library.
    if err := pdf.Init(); err != nil {
        log.Fatal(err)
    }
    defer pdf.Destroy()

    // Create object from URL.
    object, err := pdf.NewObject("https://google.com")
    if err != nil {
        log.Fatal(err)
    }
    object.EnableJavascript = false

    // Create converter.
    converter, err := pdf.NewConverter()
    if err != nil {
        log.Fatal(err)
    }
    defer converter.Destroy()

    // Add created objects to the converter.
    converter.Add(object)

    // Convert objects and save the output PDF document.
    outFile, err := os.Create("out.pdf")
    if err != nil {
        log.Fatal(err)
    }
    defer outFile.Close()

    // Run converter.
    if err := converter.Run(outFile); err != nil {
        log.Fatal(err)
    }
}
zjkyz8 commented 1 year ago

Hi @adrg

After I disable javascript,converting still hang up at 10%. but if i do pdf.Init() before every convert, that will be ok. so do i need to do init before every convert? By the way, I use a Gin to setup a web server with other functions.

Thanks a lot

adrg commented 1 year ago

After I disable javascript,converting still hang up at 10%. but if i do pdf.Init() before every convert, that will be ok. so do i need to do init before every convert? By the way, I use a Gin to setup a web server with other functions.

No, calling pdf.Init() before every conversion will most likely not work consistently.

wkhtmltox has the limitation that it has to be called from the main thread. This is not a limitation of this package, but of the original library. Please see the http-server example for more information.

zjkyz8 commented 1 year ago

I use mainthread to make all logic run in main thread. If I call pdf.Init() one time when app staring up instead of call it before every convert, i found it will hang up at 10% without any error. Do you have any idea about this weird situation?

adrg commented 1 year ago

I'm not sure. It's hard to tell without seeing your code. wkhtmltox usually hangs if the initialization and conversion is not done on the main thread. Does the http-server example work well for you? If it does, you could try to adapt the code in that example to work with Gin.