2

对不起,我的英语不是很好。我希望你能帮助我。我有这个来自 xslt trans. 的字符串:

"<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<html>\r\n  <head />\r\n  <body style=\"font-family: Verdana, sans-serif;\">\r\n  ĚŠČŘŽŘÝŽÝÁÁÍÉŮ ěščřžýáíéůú\r\n       </body>\r\n</html>"

当我尝试将此html字符串转换为pdf时:

private static byte[] CreatePdfUsingXslt(string htmlText)
    {
        var msOutput = new MemoryStream();
        var reader = new StringReader(htmlText);

        var document = new Document(PageSize.A4, 30, 30, 30, 30);
        var pdfWriter = PdfWriter.GetInstance(document, msOutput);
        var worker = new HTMLWorker(document);

        document.Open();
        worker.StartDocument();
        worker.Parse(reader);
        worker.EndDocument();
        worker.Close();
        document.Close();

        return msOutput.GetBuffer();
    }

protected void SavePDF(byte[] storeData, string fileName)
    {
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();

        Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
        Response.ContentType = "application/pdf";
        Response.Charset = Encoding.UTF8.ToString();
        Response.ContentEncoding = Encoding.UTF8;
        Response.Buffer = true;

        Response.BinaryWrite(storeData);
        Response.Flush();
        Response.End();
    }

我得到了这样的东西:ŠŽÝŽÝÁÁÍÉ šžýáíéú这是错误的......我的结果没有像Ě,Č,Ř,Ů,...这些是捷克语字符这样的字符。


我尝试像这样使用 XMLParser:

private static byte[] CreatePdfUsingXslt(string htmlText, string serverPath)
    {
        var msInput = new MemoryStream(Encoding.UTF8.GetBytes(htmlText));
        var msOutput = new MemoryStream();

        msInput.Position = 0;
        msOutput.Position = 0;

        var doc = new Document(PageSize.A4, 30, 30, 30, 30);
        PdfWriter pdfWriter = PdfWriter.GetInstance(doc, msOutput);

        var htmlPipelineContext = new HtmlPipelineContext();
        var cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);

        var cssPath = String.Format("{0}XsltTemplates\\print.css", serverPath);

        cssResolver.AddCssFile(cssPath,true);

        var pipeline = new CssResolverPipeline(cssResolver,
                                               new HtmlPipeline(htmlPipelineContext, new PdfWriterPipeline(doc, pdfWriter)));
        var xmlWorker = new XMLWorker(pipeline, true);
        var xmlParser = new XMLParser(true, xmlWorker);

        xmlParser.Parse(new StreamReader(msInput, Encoding.UTF8));
        xmlParser.Flush();

        doc.Close();
        return msOutput.ToArray();
    }

但函数xmlParser.Parse(new StreamReader(msInput, Encoding.UTF8));抛出 NullReferenceException。

这对我来说更重要。你有任何想法,我如何解决它?

4

0 回答 0