Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to disable-javascript config #31

Open
zjkyz8 opened this issue Feb 5, 2023 · 5 comments
Open

how to disable-javascript config #31

zjkyz8 opened this issue Feb 5, 2023 · 5 comments

Comments

@zjkyz8
Copy link

zjkyz8 commented Feb 5, 2023

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
Copy link
Owner

adrg commented Feb 5, 2023

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
Copy link
Author

zjkyz8 commented Feb 6, 2023

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
Copy link
Owner

adrg commented Feb 6, 2023

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
Copy link
Author

zjkyz8 commented Feb 7, 2023

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
Copy link
Owner

adrg commented Feb 8, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants