'How to set Password Protected Pages on WeasyPrint PDF Builder?

I want to set Password Protected Pages onWeasyPrint PDF Builder.

I am generated PDF using Weasyprint in Django and I want password encrypted after download PDF File.

** Here is code for generate PDF file.**


def build_pdf(request,html_content, header_html=None, footer_html=None):

    def get_page_body(boxes):
        for box in boxes:
            if box.element_tag == "body":
                return box

            return get_page_body(box.all_children())

    def get_page_and_body(html, css):
        if html is None:
            return None
        html = weasyprint.HTML(
            string=html,
            base_url=getattr(settings, "WEASYPRINT_BASEURL", None),
            url_fetcher=django_url_fetcher,
        )
        css += "@page { margin 0 !important; }"
        document = html.render(stylesheets=[weasyprint.CSS(string=css)])

        document_page = document.pages[0]
        document_body = get_page_body(document_page._page_box.all_children())
        return (
            document_page,
            document_body.copy_with_children(document_body.all_children()),
        )

    def preprocess_html(html, context):
        for key, value in context.items():
            html = html.replace(f"{{{{ {key} }}}}", str(value))            
        return html

    document = weasyprint.HTML(
        string=html_content,
        base_url=request.build_absolute_uri(),
        # base_url=getattr(settings, "WEASYPRINT_BASEURL", None),
        url_fetcher=django_url_fetcher,
    ).render()
            
    return document.write_pdf()

can anyone help me?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source