Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 生成使用jsPDF保留HTML页面样式的pdf_Javascript_Html_Css_Pdf_Jspdf - Fatal编程技术网

Javascript 生成使用jsPDF保留HTML页面样式的pdf

Javascript 生成使用jsPDF保留HTML页面样式的pdf,javascript,html,css,pdf,jspdf,Javascript,Html,Css,Pdf,Jspdf,我正在尝试创建一个按钮,该按钮将启动页面的PDF自动下载,因为它看起来与sass样式相同。然而,我尝试的每件事最终都会导致风格混乱 这是页面(这是一个测试站点,有几种不同的内容类型) 但PDF显示如下: 我正在拉取jspdf.debug.js,页面中有以下HTML按钮+脚本: <div id="bypass"> <!-- keeps button from showing in PDF --> <button id="pdf-new" style="ma

我正在尝试创建一个按钮,该按钮将启动页面的PDF自动下载,因为它看起来与sass样式相同。然而,我尝试的每件事最终都会导致风格混乱

这是页面(这是一个测试站点,有几种不同的内容类型)

但PDF显示如下:

我正在拉取
jspdf.debug.js
,页面中有以下HTML按钮+脚本:

<div id="bypass"> <!-- keeps button from showing in PDF -->
    <button id="pdf-new" style="margin: 50px;"><a href="javascript:demoFromHTML()" class="button" style="color: black;">Generate PDF</a></button>
</div>

<script>
    function demoFromHTML() {
        var pdf = new jsPDF('p', 'pt', 'letter');
        // source can be HTML-formatted string, or a reference
        // to an actual DOM element from which the text will be scraped.
        source = $('#content')[0];

        // we support special element handlers. Register them with jQuery-style 
        // ID selector for either ID or node name. ("#iAmID", "div", "span" etc.)
        // There is no support for any other type of selectors 
        // (class, of compound) at this time.
        specialElementHandlers = {
            // element with id of "bypass" - jQuery style selector
            '#bypass': function (element, renderer) {
                // true = "handled elsewhere, bypass text extraction"
                return true
            }
        };
        margins = {
            top: 80,
            bottom: 60,
            left: 40,
            width: 522
        };
        // all coords and widths are in jsPDF instance's declared units
        // 'inches' in this case
        pdf.fromHTML(
        source, // HTML string or DOM elem ref.
        margins.left, // x coord
        margins.top, { // y coord
            'width': margins.width, // max width of content on PDF
            'elementHandlers': specialElementHandlers
        },

        function (dispose) {
            // dispose: object with X, Y of the last line add to the PDF 
            //          this allow the insertion of new lines after html
            pdf.save('Test.pdf');
        }, margins);
    }
</script>

函数demoFromHTML(){
var pdf=新的jsPDF(“p”、“pt”、“字母”);
//源可以是HTML格式的字符串或引用
//到实际的DOM元素,文本将从该元素中删除。
来源=$(“#内容”)[0];
//我们支持特殊的元素处理程序。用jQuery风格注册它们
//ID或节点名称的ID选择器。(“#iAmID”、“div”、“span”等)
//不支持任何其他类型的选择器
//(类,指化合物)此时。
SpecialElementHandler={
//id为“bypass”的元素-jQuery样式选择器
“#旁路”:函数(元素、渲染器){
//true=“在别处处理,绕过文本提取”
返回真值
}
};
边距={
排名:80,
底数:60,
左:40,,
宽度:522
};
//所有坐标和宽度都以jsPDF实例声明的单位表示
//在本例中为“英寸”
pdf.fromHTML(
source,//HTML字符串或DOM元素引用。
margins.left,//x坐标
margins.top,{//y坐标
“宽度”:margins.width,//PDF上内容的最大宽度
“elementHandlers”:specialElementHandlers
},
功能(处置){
//dispose:将最后一行的X,Y添加到PDF中的对象
//这允许在html之后插入新行
保存('Test.pdf');
},利润率);
}

如何使样式从html到pdf保持一致?

我最终通过与一起使用实现了这一点

使用html2canvas选项对pdf隐藏自己的我的按钮:

<div data-html2canvas-ignore="true">
    <button id="pdf-new"><a href="javascript:demoFromHTML()" class="button" style="color: black;">Generate PDF</a></button>
</div>
_html2canvas.Util.isTransparent = function(backgroundColor) {
  return (backgroundColor === "transparent" || backgroundColor === "rgba(0, 0, 0, 0)");
};
为此,为了避免透明到黑色的背景

_html2canvas.Util.isTransparent = function(backgroundColor) {
  return (backgroundColor === "transparent" || backgroundColor === "rgba(0, 0, 0, 0)" || backgroundColor === undefined);
};

希望这对别人有帮助

有一个名为pd4ml的java库,它的设计就是为了实现这一功能。CSS是否直接应用于内联元素?如果是的话,可能会更好。@QuestionMarks如果这不起作用,我可以试试,但是jsPDF也是为此设计的,所以我希望能解决我的问题。我尝试过的每个解决方案都有这个造型问题。@Matthew我试过做内联样式,但也没用。这能保留链接和图形吗?你救了我一天,非常感谢
_html2canvas.Util.isTransparent = function(backgroundColor) {
  return (backgroundColor === "transparent" || backgroundColor === "rgba(0, 0, 0, 0)" || backgroundColor === undefined);
};