简体   繁体   English

使用reportlab生成的pdf文件,另存为纯文本

[英]pdf file generated using reportlab , getting saved as plain text

this is the first time i'm using reportlab. 这是我第一次使用reportlab。 i copied the exact code from django documentation. 我从Django文档中复制了确切的代码。 https://docs.djangoproject.com/en/2.1/howto/outputting-pdf/ . https://docs.djangoproject.com/en/2.1/howto/outputting-pdf/ When i save the file its getting saved as plain text document (text/plain), the name remains the same hello.pdf and there is no text. 当我保存文件时,将其另存为纯文本文档(文本/纯文本),名称保持不变hello.pdf且没有文本。

p = canvas.Canvas(buffer) in this line if i write the name of file 'hello.pdf' instead of buffer and remove the buffer from the fileresponse method it works and automatically gets saved as pdf file, but i cannot prompt the user to save the file and there are two pages in the pdf. p = canvas.Canvas(buffer)在这一行中,如果我写了文件'hello.pdf'的名称而不是buffer并从它起作用的fileresponse方法中删除了该缓冲区并自动保存为pdf文件,但是我无法提示用户保存文件,并且pdf中有两页。

def some_view(request):
    # Create a file-like buffer to receive PDF data.
    buffer = io.BytesIO()

    # Create the PDF object, using the buffer as its "file."
    p = canvas.Canvas(buffer)

    # Draw things on the PDF. Here's where the PDF generation happens.
    # See the ReportLab documentation for the full list of functionality.
    p.drawString(100, 100, "Hello world.")

    # Close the PDF object cleanly, and we're done.
    p.showPage()
    p.save()

    # FileResponse sets the Content-Disposition header so that browsers
    # present the option to save the file.
    return FileResponse(buffer, as_attachment=True, filename='hello.pdf')

i tried specifying the content_type='application/pdf' in the code provided by django documentation but it still gets saved as plain text document. 我尝试在Django文档提供的代码中指定content_type ='application / pdf',但仍将其保存为纯文本文档。 i'm guessing the File response cannot guess the type of file from the filename argument as mentioned in django documentation. 我猜想文件响应不能从Django文档中提到的filename参数中猜测文件的类型。

class FileResponse(open_file, as_attachment=False, filename='', **kwargs) if open_file doesn't have a name or if the name of open_file isn't appropriate, provide a custom file name using the filename parameter. class FileResponse(open_file,as_attachment = False,filename ='',** kwargs)如果open_file没有名称或open_file的名称不合适,请使用filename参数提供自定义文件名。

The as_attachment and filename keywords argument were added. 添加了as_attachment和filename关键字参数。 Also, FileResponse sets the Contentheaders if it can guess them. 另外,如果FileResponse可以猜出ContentHeader,则会设置ContentHeader。

If i use the code from 2.0 django documentation it works. 如果我使用2.0 django文档中的代码,它将起作用。 is there a bug in the latest django documentation 2.1? 最新的django文档2.1中是否有bug? i installed all the dependencies according to this official link https://bitbucket.org/rptlab/reportlab/src/927995d54048767531a4ad4a0648e46064b0c4c7/README.txt?at=default&fileviewer=file-view-default environment- ubuntu 18.04lts , pycharm, Python 3.5.6, reportlab 3.5.12. 我根据此官方链接https://bitbucket.org/rptlab/reportlab/src/927995d5404876753131a4ad4a0648e46064b0c4c7/README.txt?at=default&fileviewer=file-view-default环境-ubuntu 18.04lts,pycharm,Python 3.5安装了所有依赖项。 6,reportlab 3.5.12。

You need to reset the buffer position to start before returning the FileResponse: 您需要将缓冲区位置重置为开始才能返回FileResponse:

buffer.seek(io.SEEK_SET)

Otherwise the buffer will be read starting at its end (after the canvas was written) and an empty file is returned. 否则,将从缓冲区的结尾(在写入画布之后)开始读取缓冲区,并返回一个空文件。

This is missing in the documentation since v2.1 and should be fixed. 自v2.1以来,该文档中已丢失此问题,应予以修复。

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

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