How to use external css in tcpdf pdf generation

后端 未结 2 963
梦如初夏
梦如初夏 2021-02-13 10:35

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.

Can anyone help me with this.

相关标签:
2条回答
  • 2021-02-13 10:53

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

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

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

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

    0 讨论(0)
  • 2021-02-13 11:09
    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;
            }
    
    0 讨论(0)
提交回复
热议问题