【问题标题】:How to create a PNG image from HTML page and save it to the database using Django?如何从 HTML 页面创建 PNG 图像并使用 Django 将其保存到数据库中?
【发布时间】:2020-03-06 09:45:00
【问题描述】:

我是 Django 新手。 所以基本上我已经在我的 HTML 页面中创建了一个卡片视图。

当我单击下载按钮时,应创建 HTML 卡的图像文件 (PNG)(如图所示)并将其保存到数据库中。 下载的图像应该在 django 项目的 media/cardimage 文件夹中。 图片如下所示。(截图)card image HTML代码如下所示

我怎样才能完成这项任务? 非常感谢任何人的帮助。

谢谢。

<div class="container">
        <div class="col" >
            <div class="col-lg-4 d-flex align-items-stretch" >
                <div id="html-content-holder" class="card rounded-lg shadow bg-white rounded" style="width: 25rem; width: 50vw; margin:auto;">
                    <div class="overlay">
                        <img class="card-img-top" src="{% static 'images/ab_nba_040320.jpg' %}" alt="Card image cap">
                    </div>
                    <!-- Card content -->
                    <div class="card-body">
                        <!-- Title -->
                        <div style="text-align: left;">
                            <h4 class="card-title font-weight-bold align-left">
                                <p style="font-size: 80%;">NBA: Davis sparks Lakers over Sixers with hot second quarter, LeVert hits career-best 51pts as Nets beat Celtics
                                    <a id="btn-Convert-Html2Image" class="fa fa-download" aria-hidden="true" style="cursor: pointer;" title="Download"></a>
                                </p>
                            </h4>
                            <!-- Text -->
                            <p class="card-text" style="font-size: 85%;">Anthony Davis produced a devastating burst of scoring, as the Los Angeles Lakers came from behind to defeat the Philadelphia 76ers 120-107 on Tuesday (March 3) in their National Basketball Association (NBA) game.
                                He led the Lakers' surge with 37 points at the Staples Centre after the hosts recovered from a slow start to run out convincing winners.</p>
                            <p style="font-size: 70%; opacity: 30%;">Published Date: 05 Mar 2020</p>
                        </div>
                        <img class="" style="width: 30%; height: auto; margin-bottom: -5%;" src="{% static 'images/cover.png' %}" alt="Card image cap">
                    </div>
                </div>
            </div>
        </div>
    </div>

【问题讨论】:

    标签: python django


    【解决方案1】:

    如果您想将容器的内容保存在 HTML 中,您可以使用带有 Canvas 的 HTML,例如:

    $(function() { 
        $("#btnSave").click(function() { 
            html2canvas($("#widget"), {
                onrendered: function(canvas) {
                    theCanvas = canvas;
    
    
                    canvas.toBlob(function(blob) {
                        saveAs(blob, "Dashboard.png"); 
                    });
                }
            });
        });
    });
    

    查看 fiddle http://jsfiddle.net/6FZkk/1/ 上的完整示例

    【讨论】:

    • 错误 -> 未捕获(承诺中)错误:元素未附加到文档
    • 您可以使用 HTML2CANVAS 从 html 页面创建 png 图像。请在此处查看文档html2canvas.hertzen.com/documentation
    猜你喜欢
    • 2020-12-14
    • 2021-10-15
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    相关资源
    最近更新 更多