简体   繁体   English

如何更改 pdf 中由 pisa 生成的页面大小

[英]How to change page size in pdf generated by pisa

I am creating pdf from my html page using pisa in django framework.我在 django 框架中使用 pisa 从我的 html 页面创建 pdf。 Can anyone please tell me how to adjust the pdf page size.谁能告诉我如何调整 pdf 页面大小。 I tried to use A5 but its not reflecting.我尝试使用 A5 但它没有反映。 Please find the code below:请找到下面的代码:

       context_dict = {
            'pagesize':'A5',
            'result_detail': obj.result_object(slug, roll_no),
            'roll_no': roll_no
       }
       template = get_template('widgets/result.html')
       context = Context(context_dict)
       html  = template.render(context)
       result = StringIO.StringIO()
       pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)

You can do it by overloading a default CSS definition. 您可以通过重载默认CSS定义来实现。
To get the current default CSS use the command line tool like this: 要获取当前的默认CSS,请使用如下命令行工具:

xhtml2pdf --css-dump > /path-to-your-project/default.css

Then, open the resulting file and insert the following line at the end: 然后,打开生成的文件,并在末尾插入以下行:

@page { size: A5 }

In your code, you can use the resulting file as follows: 在代码中,您可以按以下方式使用生成的文件:

pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result,  default_css=open('default.css','r').read())

Simply writing the snippet below in the style tag, did the trick for me.只需在 style 标签中写下下面的代码片段,就可以解决问题。

@page { size: A5 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM