reportlab:如何设置初始/默认视图?

时间:2017-07-04 17:16:36

标签: python django pdf reportlab

我成功生成了一个empy PDF,但它没有设置初始视图。我想将初始视图设置为“完整视图”,即最终用户看到一页适合PDF阅读器(= A4页面适合屏幕)。

def render_to_response(self, context, **response_kwargs):

    response = HttpResponse(content_type='application/pdf; charset=utf-8')
    response['Content-Disposition'] = 'attachment; filename=""'

    p = canvas.Canvas(response, pagesize=A4, )
    p.showPage()
    p.save()

    return response

如何使用reportlab设置默认缩放视图(如果可能)?

1 个答案:

答案 0 :(得分:0)

简而言之

添加以下行:

p.setViewerPreference("FitWindow", "true")

解释

使用以下设置查看器首选项:

def setViewerPreference(self,pref,value):
    set one of the allowed enbtries in the documents viewer preferences

可用prefvalue是:

class ViewerPreferencesPDFDictionary(CheckedPDFDictionary):
    validate=dict(
        HideToolbar=checkPDFBoolean,
        HideMenubar=checkPDFBoolean,
        HideWindowUI=checkPDFBoolean,
        FitWindow=checkPDFBoolean,
        CenterWindow=checkPDFBoolean,
        DisplayDocTitle=checkPDFBoolean,    #contributed by mark Erbaugh
        NonFullScreenPageMode=checkPDFNames(*'UseNone UseOutlines UseThumbs UseOC'.split()),
        Direction=checkPDFNames(*'L2R R2L'.split()),
        ViewArea=checkPDFNames(*'MediaBox CropBox BleedBox TrimBox ArtBox'.split()),
        ViewClip=checkPDFNames(*'MediaBox CropBox BleedBox TrimBox ArtBox'.split()),
        PrintArea=checkPDFNames(*'MediaBox CropBox BleedBox TrimBox ArtBox'.split()),
        PrintClip=checkPDFNames(*'MediaBox CropBox BleedBox TrimBox ArtBox'.split()),
        PrintScaling=checkPDFNames(*'None AppDefault'.split()),
        )

参考