php 如何在tcpdf pdf生成中使用外部css

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19002634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:38:46  来源:igfitidea点击:

How to use external css in tcpdf pdf generation

phptcpdf

提问by user1559230

I am trying to create a pdf of a web page with tcpdf. But it's not working. The page is a php with external css and javascript files.

我正在尝试使用 tcpdf 创建网页的 pdf。但它不起作用。该页面是一个带有外部 css 和 javascript 文件的 php。

Can anyone help me with this.

谁能帮我这个。

Thanks,

谢谢,

回答by Saravanan

To include external CSS file, you can do as below before you add your HTMLcontent

要包含外部 CSS 文件,您可以在添加HTML内容之前执行以下操作

$html .= '<style>'.file_get_contents(_BASE_PATH.'stylesheet.css').'</style>';

By this, while you pass $htmlto generate pdf it will include those styles.

这样,当您通过$html生成 pdf 时,它将包含这些样式。

As far I am aware, there is no need for including Javascriptinto a PDF. The purpose of a PDF is to display a non-interactive static content, which can be achieved by HTMLand CSS

据我所知,没有必要JavascriptPDF. PDF 的目的是显示非交互式静态内容,这可以通过HTMLCSS

回答by sandeep agarwal

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;
        }