【问题标题】:django wkhtmltopdf-different page orientation in single pdfdjango wkhtmltopdf-单个pdf中不同的页面方向
【发布时间】:2015-05-04 09:48:22
【问题描述】:

我想使用 wkhtmltopdf 创建一个具有不同方向的 pdf(例如:一个页面是纵向的,另一页是横向的,然后是纵向的),我在 css 中尝试过,但它不起作用。请帮助,在此先感谢.

我的意见.py

from django.views.generic.base import View
from wkhtmltopdf.views import PDFTemplateResponse

class pdf_view(View):
    template = 'temp1.html'
    filename = 'file1.pdf'
    header_template='header_temp.html'
    footer_template='footer_temp.html'
    context= {'title': 'PDF GENERATION'}

    def get(self, request):
        response = PDFTemplateResponse(request=request,
                                   template=self.template,
                                   filename = self.filename,
                                   header_template=self.header_template,
                                   footer_template=self.footer_template,
                                   context= self.context,
                                   )
        return response

我的模板.html

<html>
 <head>
    <meta charset="utf-8">
      <title>{{ title }}</title>
        <style type="text/css">
               #landimg img{
                   margin-top:20px;
                   height:auto;
                   width:auto;
                   }
               #portimg img{
                   height:auto;
                   width:auto;
                   margin-top:50px;
                   }
               @page #port{
                   size:letter;
                   }
               @page #land{
                   size:landscape;
                   }
           </style>
        </head>
       <body>
        <div id='port'>
           <img src="/static/images/2_180_3.jpg" alt="" />
       </div>
        <div id='land'>
             <img src="/static/images/land.JPG" alt="" />
        </div>  
       </body>
     </html>

@page css 在打印 pdf 时不起作用,默认为纵向。

【问题讨论】:

    标签: django pdf wkhtmltopdf


    【解决方案1】:

    这应该适合你

     @page{
          size:letter;
     }
     @page land{
           size:landscape;
     }
    

    在 wkhtmltopdf 中,只有少数 css 属性可用于 UI 设计。

    <div>
         <img src="/static/images/2_180_3.jpg" alt="" />
    </div>
    <div>
      <pdf:nexttemplate name="land"/> 
      <pdf:nextpage/>   
    </div>
    <div>
       <img src="/static/images/land.JPG" alt="" />
    </div>
    

    【讨论】:

    • 谢谢@Amar Kamthe,我试过你的css代码,但所有页面仍然只有纵向,横向不起作用。
    • 我想知道是否有可能使用 wkhtmltopdf 在单个文档中同时使用纵向和横向,文档说 cmd_options 的方向是全局的,所以我把 cmd_options = {'orientation' : 'landscape /portrait'} 它适用于单一方向,但我需要两者。
    • @Sansiva :我尝试过使用比萨模块。它是 html 2 pdf 转换器。在我的情况下,单个 pdf 有纵向和横向页面。请参阅stackoverflow.com/questions/1377446/… 上的第一个答案。这是如何使用 pisa 的示例,然后您可以添加上面的代码用于横向和纵向视图。
    • 嗨,Amar,我使用了 pisa 模块,正如您所说,它工作得很好,非常感谢.. 但是 .svg 格式的图像不会以 pdf 格式显示,有什么方法可以显示 svg 图像而不转换为jpeg,png??
    • 完全不用担心@Sansiva,很高兴我能帮上忙。关于 svg 图像,似乎没有办法使用 pisa 模块显示 svg 图像。我一直致力于显示 png。
    猜你喜欢
    • 1970-01-01
    • 2012-10-10
    • 2017-03-11
    • 2015-04-03
    • 1970-01-01
    • 2021-10-13
    • 2013-03-03
    • 2010-10-14
    • 1970-01-01
    相关资源
    最近更新 更多