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

panic happens #18

Closed machelcheng closed 2 years ago

machelcheng commented 2 years ago

panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x4433cf4]

goroutine 1 [running]: github.com/adrg/go-wkhtmltopdf.(*objectRegistry).add(0x0, 0x7b117c0, 0x44daf40, 0xc0001c3ab0) /Users/machel/gopath/pkg/mod/github.com/adrg/go-wkhtmltopdf@v0.3.0/object_registry.go:39 +0x34 github.com/adrg/go-wkhtmltopdf.NewConverterWithOpts(0xc0001ab440, 0xc0001a9e90, 0xc00000ed40, 0x0) /Users/machel/gopath/pkg/mod/github.com/adrg/go-wkhtmltopdf@v0.3.0/converter.go:276 +0x1e5 github.com/adrg/go-wkhtmltopdf.NewConverter(...) /Users/machel/gopath/pkg/mod/github.com/adrg/go-wkhtmltopdf@v0.3.0/converter.go:235 main.HtmlCvtPdf(0x459dc00, 0xc00002c0a8, 0xc00002cb6c, 0x4, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0)

machelcheng commented 2 years ago

this is my test program, something wrong?

func HtmlCvtPdf(ctx context.Context, htmlBytes []byte) (error, []byte) { reader := bytes.NewReader(htmlBytes) object, err := pdf.NewObjectFromReader(reader) if err != nil { log.Errorf("pdf NewObjectFromReader failed:%+v", err) return err, []byte("") }

// Create converter.
converter, err := pdf.NewConverter()
if err != nil {
    log.Errorf("pdf NewConverter failed:%+v", err)
    return err, []byte("")
}
defer converter.Destroy()

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

outBuffer := bytes.NewBuffer(nil)
writer := bufio.NewWriter(outBuffer)
if err := converter.Run(writer); err != nil {
    log.Errorf("pdf converter Run failed:%+v", err)
    return err, []byte("")
}

if len(outBuffer.Bytes()) > 0 {
    return nil, outBuffer.Bytes()
} else {
    return fmt.Errorf("convert failed, out buffer len is 0"), []byte("")
}

}

adrg commented 2 years ago

Hi @machelcheng. Thank you for your interest in the library. Did you perhaps forget to initialize the library?

if err := pdf.Init(); err != nil {
    log.Fatal(err)
}
defer pdf.Destroy()

You only need to initialize the library once, somewhere at the beginning of your main function for example.

machelcheng commented 2 years ago

ok, i will try

adrg commented 2 years ago

Sure, please let me know how it goes.

machelcheng commented 2 years ago

After adding it is ok,but new panic happens,do you know what's going on?

machelcheng commented 2 years ago

QIconvCodec::convertFromUnicode: using Latin-1 for conversion, iconv_open failed QIconvCodec::convertToUnicode: using Latin-1 for conversion, iconv_open failed signal 17 received but handler not on signal stack fatal error: non-Go code set up signal handler without SA_ONSTACK flag

runtime stack: runtime: unexpected return pc for runtime.sigtramp called from 0x7fb9a25f4630 stack: frame={sp:0xc00027eee8, fp:0xc00027ef40} stack=[0xc000276e38,0xc00027f238) 000000c00027ede8: 000000c00027edf0 000000000045e450 <runtime.throw.func1+0> 000000c00027edf8: 0000000001340c7b 0000000000000039 000000c00027ee08: 000000c00027ee28 0000000000447bff <runtime.sigNotOnStack+127> 000000c00027ee18: 0000000001340c7b 0000000000000039 000000c00027ee28: 000000c00027eed8 00000000004471ab <runtime.sigtrampgo+747> 000000c00027ee38: 0000000000000011 000000c00027eec0 000000c00027ee48: 000000c00027ef40 0000000000000000 000000c00027ee58: 0000000000000000 000000c00027eee8 000000c00027ee68: 0000000000000000 0000000000000000 000000c00027ee78: 0000000000000000 0000000000000000 000000c00027ee88: 0000000000000000 000000c000099380 000000c00027ee98: 0000000000000000 0000000000000000 000000c00027eea8: 0000000000000000 0000000000000000 000000c00027eeb8: 0000000000000000 000000c0013d2000 000000c00027eec8: 0000000000000000 0000000000008000 000000c00027eed8: 000000c00027ef30 0000000000463953 <runtime.sigtramp+67> 000000c00027eee8: <0000000000000011 000000c00027f070 000000c00027eef8: 000000c00027ef40 0000000000000055 000000c00027ef08: 0000000000000033 0000000000000034 000000c00027ef18: ffffffffffffffff 000000c00027ef30 000000c00027ef28: 000000c00004c000 000000c00027fd18 000000c00027ef38: !00007fb9a25f4630 >0000000000000007 000000c00027ef48: 0000000000000000 000000c0013d2000 000000c00027ef58: 0000000000000000 0000000000008000 000000c00027ef68: 0000000000000000 0000000000000000 000000c00027ef78: 0000000001000004 0000000000000206 000000c00027ef88: ffffffffffffffff 0000000000000034 000000c00027ef98: 0000000000000033 0000000000000055 000000c00027efa8: 0000000000000001 000000000025fff9 000000c00027efb8: 000000c00027fd18 000000c00004c000 000000c00027efc8: 000000c00027fc88 0000000000000000 000000c00027efd8: 000000000047de9a <syscall.Syscall6+42> 000000c00027fc30 000000c00027efe8: 000000000047de9a <syscall.Syscall6+42> 0000000000000206 000000c00027eff8: 002b000000000033 0000000000000000 000000c00027f008: 0000000000000000 0000000000000000 000000c00027f018: 0000000000000000 000000c00027f100 000000c00027f028: 0000000000000000 0000000000000000 000000c00027f038: 0000000000000000 runtime.throw(0x1340c7b, 0x39) /usr/local/go/src/runtime/panic.go:774 +0x72 runtime.sigNotOnStack(0x11) /usr/local/go/src/runtime/signal_unix.go:578 +0x7f 390495,1 99%

adrg commented 2 years ago

This does not seem related to this project. What file/URL are you trying to convert to PDF? What's your version of wkhtmltopdf and how did you install it?

machelcheng commented 2 years ago

hello adrg:

wkhtmltopdf -V

QIconvCodec::convertFromUnicode: using Latin-1 for conversion, iconv_open failed wkhtmltopdf 0.12.6 (with patched qt)

i install wkhtmltopdf with this pkg: https://wkhtmltopdf.org/downloads.html centos 7 x86-64

and install dependent libraries: yum install -y libX11-1.6.3 yum install -y libXext-1.3.3 yum install -y libXrender-0.9.8 yum install -y openssl-1.0.2k yum install -y xorg-x11-fonts-75dpi-7.5 RUN yum install -y xorg-x11-fonts-Type1-7.5

machelcheng commented 2 years ago

it seems a problem with the installed package?

adrg commented 2 years ago

Not sure yet. What are you trying to convert? Does it work if you are using the wkhtmltopdf binary in the terminal?

machelcheng commented 2 years ago

$ wkhtmltopdf You need to specify at least one input file, and exactly one output file Use - for stdin or stdout

QIconvCodec::convertFromUnicode: using Latin-1 for conversion, iconv_open failed

machelcheng commented 2 years ago

it seems execution binary also has this error

adrg commented 2 years ago

It seems that way, yes. But try running it anyway. Does this work?

wkhtmltodpf https://www.google.com out.pdf
machelcheng commented 2 years ago

yes, it works, bug my test program is using the interface to convert, not binary

machelcheng commented 2 years ago

Can i use this libinary to convert to image format?

adrg commented 2 years ago

I know. I was just trying to figure out if wkhtmltox was correctly installed. Can you try this basic usage sample?

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 file.
    object, err := pdf.NewObject("https://www.google.com")
    if err != nil {
        log.Fatal(err)
    }

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

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

    // Set converter options.
    converter.Title = "Sample document"
    converter.PaperSize = pdf.A4

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

    if err := converter.Run(outFile); err != nil {
        log.Fatal(err)
    }
}
machelcheng commented 2 years ago

ok, i trying it

machelcheng commented 2 years ago

hello, adrg it works

adrg commented 2 years ago

Ok, so you have a problem in your application. Are you trying to perform conversion inside a Goroutine? If so, take a look at HTTP server. The solution is explained in that example.

machelcheng commented 2 years ago

yes, i perform conversion inside a goroutine, i have seen this example, I probably know, thank you very much! but i have two questions:

  1. I would like to know if there are other ways to shield this signal from reporting errors, not use the solution in that http server example?

2.I would like to ask if this package can be converted into image format?

adrg commented 2 years ago

Hi @machelcheng

  1. I would like to know if there are other ways to shield this signal from reporting errors, not use the solution in that http server example?

Unfortunately no, that's the only solution I know of. This is a limitation of wkhtmltox, not of this library (I linked the relevant discussions and issues on this in my HTTP server example).

2.I would like to ask if this package can be converted into image format?

No, this package is only for HTML to PDF conversion. I have been considering adding a PDF to image package, separate from this one.

adrg commented 2 years ago

Closing this issue as it has been addressed. Please reopen if there are any issues with the provided solution.