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