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

Code Issues Do you want live notifications when people reply to your posts? wkhtmltopdf not working upon dockerizing my django app #203

Open
mostafamohamed198 opened this issue Jun 4, 2023 · 0 comments

Comments

@mostafamohamed198
Copy link

I was working with my django app on my localserver and There was a view that shows me pdf using wkhtmltopdf, when I decided to dockerize my app this result is shown to me when I try to reach the pdf:
image

views.py

wkhtml_to_pdf = os.path.join(
    settings.BASE_DIR, "wkhtmltopdf.exe")


def resume_pdf(request,id):
    # return render(request, 'frontend/index.html')
    options = {
        'page-size': 'A4',
        'page-height': "13in",
        'page-width': "10in",
        'margin-top': '0in',
        'margin-right': '0in',
        'margin-bottom': '0in',
        'margin-left': '0in',
        'encoding': "UTF-8",
        'no-outline': None
    }

    template_path = 'frontend/thepdf.html'
    template = get_template(template_path)
    
    context = {"name": "Areeba Seher"}
    html = template.render(context)

    config = pdfkit.configuration(wkhtmltopdf=wkhtml_to_pdf)

    pdf = pdfkit.from_string(html, False, configuration=config, options=options)

    # Generate download
    response = HttpResponse(pdf, content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="resume.pdf"'
    theEntry = entry.objects.get(id= id)
    context = {'entry' : theEntry, 'entrybody': markdown(theEntry.body) }
    # print(response.status_code)
    if response.status_code != 200:
        return HttpResponse('We had some errors <pre>' + html + '</pre>')
    # return response
    return PDFTemplateResponse(request=request, cmd_options={'disable-javascript':True}, template=template, context=context)

my Dockerfile:

FROM python:3.10.5 as web

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

RUN wget https://s3.amazonaws.com/shopify-managemant-app/wkhtmltopdf-0.9.9-static-amd64.tar.bz2
RUN tar xvjf wkhtmltopdf-0.9.9-static-amd64.tar.bz2
RUN mv wkhtmltopdf-amd64 /usr/local/bin/wkhtmltopdf
RUN chmod +x /usr/local/bin/wkhtmltopdf


WORKDIR /app

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .
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

1 participant