简体   繁体   English

使用Javascript将CSS保存为PDF导出

[英]Keeping CSS in Html to PDF export using Javascript

I am trying to generate PDF using JavaScript but I am unable to generate on with CSS. 我正在尝试使用JavaScript生成PDF但我无法使用CSS生成。 I am currently using jsPDF . 我目前正在使用jsPDF The issue with jsPDF is that it takes away all the formatting. jsPDF的问题在于它消除了所有格式。 I don't need styles or colors but I need to have multiple divs side by side. 我不需要样式或颜色,但我需要并排多个divs

My code is 我的代码是

                    margins = {
                        top: 0,
                        bottom: 0,
                        left: 20,
                        width: 170
                    };                       
                    pdf.setFontSize(6);
                    pdf.fromHTML(
                        $('#textpdfdiv').html(), 
                        margins.left, 
                        margins.top, { 
                            'width': margins.width
                        },

                        function (dispose) {                                
                            pdf.save('Export_Text.pdf');
                        }, margins
                    );

textpdfdiv is an id of div that has all the values of form in separate divs side by side. textpdfdivdiv的id,它将所有form的值并textpdfdiv在不同的divs But when I generate PDF, they go into separate row. 但是当我生成PDF时,它们会进入单独的行。

I can't use XEPOnline because it processes online. 我无法使用XEPOnline,因为它在线处理。

How I need PDF output is something like this (minus the colors and form fields etc) 我需要PDF输出是这样的(减去颜色和表单字段等)



The output that I currently have is 我目前的输出是

Is there any plugin that converts HTML+CSS to PDF? 有没有可以将HTML + CSS转换为PDF的插件? Or can I achieve this with jsPDF? 或者我可以用jsPDF实现这个目标吗? I don't have a table so this Question doesn't help as well. 我没有桌子,所以这个问题也无济于事。

EDIT: I can't use Html2Canvas as well because it creates long non searchable PDFs and I need searchable PDFs. 编辑:我也不能使用Html2Canvas,因为它创建了长的不可搜索的PDF,我需要可搜索的PDF。 Html2Canvas takes screenshot and it is an image but I need text that can be copied. Html2Canvas截图,它是一个图像,但我需要可以复制的文本。

Thank you, 谢谢,
Hamza 哈姆扎

I used this plugin to generate PDF with HTML and CSS. 我用这个插件用HTML和CSS生成PDF。 You just have to include the content, you want to put in PDF, in a "" with an id. 您只需要在带有id的“”中包含要放入PDF的内容。

https://github.com/Xportability/css-to-pdf https://github.com/Xportability/css-to-pdf

maybe i could not to be of such help, but it seems to be a bug of that library: https://github.com/MrRio/jsPDF/issues/91 也许我不能得到这样的帮助,但它似乎是该库的一个错误: https//github.com/MrRio/jsPDF/issues/91

In that page you'll find a suggestion for your purpose: to use Html2Canvas. 在该页面中,您将找到适合您目的的建议:使用Html2Canvas。

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

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