将HTML转换为具有HTML控件和外部CSS的PDF [英] Converting HTML to PDF having HTML controls and external CSS

查看:62
本文介绍了将HTML转换为具有HTML控件和外部CSS的PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含HTML控件的HTML。在将其转换为pdf时,html控件不会呈现。它也无法在外部CSS中应用样式。请建议一种将HTML转换为符合要求的PDf的方法



我尝试过的方法:



我尝试过使用iTextSharp和PDFSharp。 iTextSharp doest渲染html控件和pdfsharp,但它有一个方法将html转换为PDF。我尝试使用html进行成像然后将图像转换为PDF但是找不到好的html到图像渲染器

I have an HTML that contains HTML controls. On converting it into pdf the html controls are not rendering. Also it fails to apply the styles in external css. Please suggest a method for converting HTML to PDf that satisfies the requirements

What I have tried:

I have tried using iTextSharp and PDFSharp. iTextSharp doest render the html controls and also pdfsharp but it doest have a method to convert html to PDF. I tried with html to image and then image to PDF but a good html to image renderer is not found

推荐答案

public byte[] GetPDF(string pHTML)
        {
            byte[] bPDF = null;

            MemoryStream ms = new MemoryStream();
            TextReader txtReader = new StringReader(pHTML);

            // 1: create object of a itextsharp document class
            Document doc = new Document(PageSize.A4, 25, 25, 25, 25);

            // 2: we create a itextsharp pdfwriter that listens to the document and directs a XML-stream to a file
            PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms);

            // 3: we create a worker parse the document
            HTMLWorker htmlWorker = new HTMLWorker(doc);

            // 4: we open document and start the worker on the document
            doc.Open();
            htmlWorker.StartDocument();

            // 5: parse the html into the document
            htmlWorker.Parse(txtReader);

            // 6: close the document and the worker
            htmlWorker.EndDocument();
            htmlWorker.Close();
            doc.Close();

            bPDF = ms.ToArray();

            return bPDF;
        }





试试这个只是传递上面的html。希望它有所帮助。



Try this just pass ur html above . Hope it helps.


这篇关于将HTML转换为具有HTML控件和外部CSS的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆