我正在尝试使用tcpdf创建网页的pdf。但它不起作用。该页面是一个带有外部css和javascript文件的php。
谁能帮我解决这个问题。
谢谢
要包含外部 CSS 文件,您可以在添加HTML
内容之前执行以下操作
$html .= '<style>'.file_get_contents(_BASE_PATH.'stylesheet.css').'</style>';
这样,当您传递$html
以生成pdf时,它将包含这些样式。
我所知,没有必要将Javascript
包含在PDF
中。PDF的目的是显示非交互式静态内容,这可以通过HTML
和CSS
来实现
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;
}