首页 文章

django wkhtmltopdf-单个pdf中的不同页面方向

提问于
浏览
1

我想用不同方向的wkhtmltopdf创建一个pdf(例如:一个页面是肖像,另一个页面是横向,再次像这样的肖像),我在css中试过但它不起作用 . 请帮忙,在此先感谢 .

我的views.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

我的template.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时无效,默认为纵向 .

1 回答

  • 0

    这应该适合你

    @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>
    

相关问题