Comments (4)
For usage inside goroutines
see the Basic web page to PDF conversion server or the Configurable web page to PDF conversion server examples. Issue #3 is also relevant.
The library must be initialized on the main thread and the conversion must be performed on the main thread as well. This is a known limitation of wkhtmltox
, not of this package.
from go-wkhtmltopdf.
Hi @zaky
The library needs to be initialized only once, when the application starts. This should work fine:
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()
for _, v := range []string{"import1.pdf", "import2.pdf", "import3.pdf"} {
if err := CreatePDFFile("header.html", "footer.html", "content.html", v); err != nil {
log.Fatal(err)
}
}
}
func CreatePDFFile(header, footer, content, output string) error {
// Create object from URLs.
object, err := pdf.NewObject(content)
if err != nil {
return err
}
defer object.Destroy()
object.Header.CustomLocation = header
object.Footer.CustomLocation = footer
// Create converter.
converter, err := pdf.NewConverter()
if err != nil {
return err
}
defer converter.Destroy()
converter.PaperSize = pdf.Letter
// Add created objects to the converter.
converter.Add(object)
// Convert objects and save the output PDF document.
outFile, err := os.Create(output)
if err != nil {
return err
}
defer outFile.Close()
// Run converter.
if err := converter.Run(outFile); err != nil {
return err
}
return nil
}
from go-wkhtmltopdf.
Actually the code is part of a web server.
What I fount is that if I put the pdf.init/pdf.destroy in main the first request is working fine but not the following requests.
From the second request the server jut stock.
from go-wkhtmltopdf.
I suppose, as mentioned in previous comment, this code block
func init() {
// Set main function to run on the main thread.
runtime.LockOSThread()
}
in general cases prevents situation with library stuck
from go-wkhtmltopdf.
Related Issues (20)
- Do I need dependencies on destination Windows PC? HOT 1
- Prerequisites Install Error HOT 5
- Two Objects with web url fail the rendering HOT 20
- Html to pdf
- panic happens HOT 22
- Undefined pdf.Object HOT 8
- Blank pdf for react website. HOT 2
- html website need token in header , how can i config ? HOT 7
- Panic WARNING: QApplication was not created in the main() thread. HOT 3
- WARNING: QApplication was not created in the main() thread. HOT 2
- Inconsistent pdf formatting and output HOT 3
- wkhtmltopdf is deprecated by its author. HOT 3
- how to disable-javascript config HOT 5
- where to set CGO_LDFLAGS and CGO_CFLAGS HOT 2
- 需要C包的支持
- how let go-wkhtmltopdf know where is wkhtmltox/pdf.h HOT 2
- How do I add header.html and footer.html to each page HOT 2
- Is it possible to use web service with HTML as base64, or escaped HTML input insted of URL's
- go-wkhtmltopdf missing wkhtmltox.dll HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-wkhtmltopdf.