首页 > 其他分享 >PDF中嵌入SVG图——reportlab生成pdf

PDF中嵌入SVG图——reportlab生成pdf

时间:2023-02-22 15:01:40浏览次数:30  
标签:reportlab scale SVG file import pdf data

由于直接使用reportlab绘制图表不是很方便,于是想到用matplotlib画图,将SVG嵌入PDF的方法。

需要用到svglib这个库,可以用pip install svglib安装

一个完整的例子:

from io import BytesIO

import matplotlib.pyplot as plt
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import SimpleDocTemplate, Paragraph
from svglib.svglib import svg2rlg


def plot_data(data):
    # Plot the data using matplotlib.
    plt.plot(data)

    # Save the figure to SVG format in memory.
    svg_file = BytesIO()
    plt.savefig(svg_file, format='SVG')

    # Rewind the file for reading, and convert to a Drawing.
    svg_file.seek(0)
    drawing = svg2rlg(svg_file)

    # Scale the Drawing.
    scale = 0.75
    drawing.scale(scale, scale)
    drawing.width *= scale
    drawing.height *= scale

    return drawing


def main():
    styles = getSampleStyleSheet()
    pdf_path = 'sketch.pdf'
    doc = SimpleDocTemplate(pdf_path)

    data = [1, 3, 2]
    story = [Paragraph('Lorem ipsum!', styles['Normal']),
             plot_data(data),
             Paragraph('Dolores sit amet.', styles['Normal'])]

    doc.build(story)


main()

标签:reportlab,scale,SVG,file,import,pdf,data
From: https://www.cnblogs.com/impromptu/p/17144311.html

相关文章

  • windows下编译pdfium
    当前流程截至2023/2/20有效1、提前安装好工具链VS2017+SDK Win10SDK10.0.20348 +Gitforwindows+tortoisegit+代理2、下载depot_tools命令行中设置环境变......
  • dokuwiki支持pdf粘帖
    ​ ueditor粘贴不能粘贴word中的图片是一个很头疼的问题,在我们的业务场景中客户要求必须使用ueditor并且支持word的图片粘贴,因为这个需求头疼了半个月,因为前端方面因为安......
  • dokuwiki支持pdf导入
    ​ 当前功能基于PHP,其它语言流程大致相同 1.新增上传wordjson配置在ueditor\php\config.json中新增如下配置:     /* 上传word配置 */    "wordAction......
  • Spire Linux Word 转PDF 字体问题
    1、首先要考虑为什么会出现这个问题呢,在windows环境下是正常的,在linux环境下就有问题了2、问题原因:Spireword转pdf会依赖环境字段,如果文档中使用字体在linux环境上没有......
  • PDF从批量解密到批量去除水印
    前言PDF批量解密工具AdobeAcrobatDC闲得无聊步骤批量去除密码从某盘下载原始加密pdf安装解密程序后直接运行pd解密程序(根据自己需求在下方修改输出文件位......
  • CSharp: Select.Pdf Free Html To Pdf Converter for .NET
    ///<summary>///SelectPdfedit:geovindu,GeovinDu///https://github.com/selectpdf////https://www.nuget.org/packages/Select.Pdf////htt......
  • vue实现预览pdf功能
    vue页面中的一部分实现预览pdf功能一、全屏预览模式先说直接全屏预览的,直接axios获取到数据,将流转为二进制文件,可以window.open(后端返回文件流,前端把流转成url,在使用w......
  • svg矢量二维码加盖在PDF文件中
    正常行驶的bitmap类型的二维码格式,加载到PDF中,将会导致二维码失真,无法扫描。矢量图可根据尺寸大小进行调节,不会出现失真模糊情况所用依赖<PackageReferenceInclude="Fr......
  • pdf转MD、HTML、word网址收集
    PDF转Word,Excel,PPT,JPG的网址:https://smallpdf.com/cn/pdf-to-wordPDF转Markdown的网址:https://pdf2md.morethan.io/PDF转HTML的网址:https://www.pdftohtml.net/......
  • 前端vue的JsPDF html2canvas 生成pdf并以文件流形式上传到后端(转载)
    原文地址1.首先在文件内引入htmlToPdf.js这里代码引入了html2canvas和jspdf//需要npmihtml2Canvas和npmijspdf在这里将getPdf这个函数挂载到Vue的原型上,最后retu......