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

Undefined pdf.Object #19

Closed sihalalbackend1 closed 2 years ago

sihalalbackend1 commented 2 years ago
Screen Shot 2022-01-21 at 22 51 05
sihalalbackend1 commented 2 years ago

trying to install in docker alpine and got error when run it, there is my docker file. need your help, thank you

RUN apk add --update --no-cache \
    git \
    alpine-sdk \
    protoc \
    libgcc \
    libstdc++ \
    libx11 \
    glib \
    libxrender \
    libxext \
    libintl \
    ttf-dejavu \
    ttf-droid \
    ttf-freefont \
    ttf-liberation \
    dpkg

COPY --from=madnight/alpine-wkhtmltopdf-builder:0.12.5-alpine3.10-866998106 \
    /bin/wkhtmltopdf /bin/wkhtmltopdf
RUN chmod +x /bin/wkhtmltopdf
#COPY  /bin/wkhtmltopdf /bin/wkhtmltopdf
#RUN dpkg -i /bin/wkhtmltopdf/wkhtmltox.deb
#RUN ldconfig

RUN wkhtmltopdf -V 
adrg commented 2 years ago

Hi @sihalalbackend1. Thank you for your interest in the library. Can you provide more information regarding what you are trying to do?

From what I can gather from the picture you attached, you don't have go-wkhtmltopdf in your Docker container. How are you retrieving the package? Do you use Go modules with your application? Are you trying to build it inside the container?

Regarding the Dockerfile, I think you can just start from the original alpine image and install the wkhtmltox development files yourself. This is what I do to install the prerequisites in one of the GitHub actions files of this repository.

sudo apt-get update
sudo apt-get -y install xfonts-75dpi xfonts-base
curl --silent --show-error --location --max-redirs 3 --fail --retry 3 --output wkhtmltopdf-linux-amd64.deb https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.bionic_amd64.deb
sudo dpkg -i wkhtmltopdf-linux-amd64.deb
sudo ldconfig
rm wkhtmltopdf-linux-amd64.deb
scor2k commented 2 years ago

Yeah, the same issue. When I'm trying to run it - all good, but I can't build it no matter where - on mac or in docker:

CGO_ENABLED=0 go build -a -o go-app .   
./generate_pdf.go:158:12: undefined: pdf.Init
./generate_pdf.go:162:9: undefined: pdf.Destroy
./generate_pdf.go:167:19: undefined: pdf.NewObject
./generate_pdf.go:177:22: undefined: pdf.NewConverter
./generate_pdf.go:186:25: undefined: pdf.A4
./generate_pdf.go:187:27: undefined: pdf.Portrait

but

go run .

works well.

adrg commented 2 years ago

Hi @scor2k. Why are your setting CGO_ENABLED to 0? This package uses cgo so it needs it to be enabled. Can you try building without setting that environment variable? If go run works well, then go build should too.

go build -a -o go-app .   
scor2k commented 2 years ago

Hi, @adrg. Thank you for the quick response. I thought I tested all possible ways to build it... but yes, you are right... I gonna try to move it to the docker then )

scor2k commented 2 years ago

I want to share the Dockerfile to build & run golang app with your module, I hope it will help other people:

FROM golang:1.15.6 as builder
ENV DIR=/go/src/github.com/xxx/app/
WORKDIR ${DIR}
RUN wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-rc/wkhtmltox_0.12.6-0.20200605.30.rc.faa06fa.stretch_amd64.deb -O wkhtmltox.deb && \
    apt-get update && \
    apt-get -y install xfonts-75dpi xfonts-base && \
    apt-get install -y --no-install-recommends fontconfig libfreetype6 libjpeg62-turbo libpng16-16 libtiff5 libwebp6 libxext6 libxrender1 libx11-6 libxcb-render0 libxcb-shm0 libxcb-xfixes0 libxcb1 libfontconfig1 libfontenc1 libfreetype6 libjpeg62-turbo libpng16-16 libtiff5 libwebp6 libxext6 libxrender1 libx11-6 libxcb-render0 libxcb-shm0 libxcb-xfixes0 libxcb1 && \
    dpkg -i wkhtmltox.deb &&  \
    ldconfig
COPY go.mod go.sum ${DIR}
RUN go mod download
COPY *.go .
RUN go build -a -o app.

FROM debian:stretch-20220228 as production
ENV DIR=/opt/app
WORKDIR ${DIR}
COPY --from=builder /go/src/github.com/xxx/app .
RUN apt-get update && \
    apt-get install -y --no-install-recommends ca-certificates wget xfonts-75dpi xfonts-base libjpeg62-turbo libx11-6 libxcb1 libxext6 libxrender1 fontconfig && \
    wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-rc/wkhtmltox_0.12.6-0.20200605.30.rc.faa06fa.stretch_amd64.deb -O wkhtmltox.deb && \
    dpkg -i wkhtmltox.deb &&  \
    ldconfig && \
    rm -f wkhtmltox.deb
VOLUME /pdf
ENTRYPOINT ["./app"]
adrg commented 2 years ago

@scor2k Thanks a lot for your contribution. It will surely be useful to other people.

adrg commented 2 years ago

Closing this issue as it has been addressed. Also, a sample Dockerfile was provided by @scor2k above. Please reopen if there are any issues with the provided solutions.