[英]Using Pdfcrowd to convert HTML to PDF in PHP
I've written the following PHP function to try and generate a PDF: 我编写了以下PHP函数来尝试生成PDF:
function createPDF($htmlContent) {
$todayDate = date('Y-m-d--H-i-s');
$this->pdfFilename = $this->upload_dir['basedir'] . '/rns/rns--'. $todayDate . '.pdf';
echo "<br><br>Creating: ". $this->pdfFilename ."<br>";
try {
// create an API client instance
$client = new Pdfcrowd($username, $apikey);
$out_file = fopen($this->pdfFilename, "wb");
echo "<br>HTML:<br><br>" . $htmlContent . "<br><br><br>";
$client->convertHtml($htmlContent, $out_file);
fclose($out_file);
}
catch(PdfcrowdException $why) {
echo "Pdfcrowd Error: " . $why . "<br>";
}
}
When run in a browser, this echos $htmlContent
correctly (displaying the HTML I want to convert, from <html>
to </html>
tag, so this variable definitely contains HTML), and then gives the following error: 当在浏览器中运行时,这个
$htmlContent
正确(显示我要转换的HTML,从<html>
到</html>
标签,所以这个变量肯定包含HTML),然后给出以下错误:
Pdfcrowd Error: [400] No data to convert. Pdfcrowd错误:[400]无法转换数据。 Missing src field.
缺少src字段。
Obviously something is wrong, but the src field is not missing as far as I can tell as it echos perfectly fine, right before it's passed to the convertHTML()
function. 显然有些东西是错的,但是在我传递给
convertHTML()
函数之前,src字段并没有丢失,因为它可以很好地convertHTML()
它。 I'm not sure how to proceed as there appears to be no other info or documentation on this error from pdfcrowd. 我不知道如何继续进行,因为pdfcrowd似乎没有关于此错误的其他信息或文档。
(Pdfcrowd documentation here ) (Pdfcrowd文档在这里 )
If I copy and paste out the echo'd html from $htmlContent
and put that into the first parameter for convertHTML()
the code works... so why can I not pass it in as a variable? 如果我复制并粘贴来自
$htmlContent
html并将其放入convertHTML()
的第一个参数中,代码可以工作......那么为什么我不能将其作为变量传递? Shouldn't it be the same? 不应该是一样的吗?
Can anyone help? 有人可以帮忙吗?
Note: I'm open to other solutions, but I have tried: html2pdf, fpdf, tcpdf, dompdf & mpdf already. 注意:我对其他解决方案持开放态度,但我已经尝试过:html2pdf,fpdf,tcpdf,dompdf和mpdf。 All have different issues rendering the html I need converting (RNS announcements from London Stock Exchange) , which can contain tables and bullet points.
所有这些都有不同的问题呈现我需要转换的html (伦敦证券交易所的RNS公告) ,其中可以包含表格和项目符号。 See my other question .
看到我的另一个问题 。 I got good results putting those RNS urls into the pdfcrowd online converter , so looks like it would work if I get round this error...
我将这些RNS网址放入pdfcrowd在线转换器中取得了很好的效果,所以看起来如果我绕过这个错误就行了...
Example of the XML Feed (see comments), these can contain more than 1 announcement: XML Feed的示例(请参阅注释),这些可以包含多于1个公告:
<Announcement id_pub="12492736" id_rns="4614Y17" cd_ann_type="POS" cd_ann_med="AFX" id_comp="" id_announcement="12492736" cd_tidm="MKS" id_cont="" cd_ann_corr="" fl_regulatory="Y" fl_copyback="" ts_rel="20150908174008" ts_ann_rec="20150908174008" cd_comp_type="" id_prev_rns="" id_par_rns_num=""> <tx_ann_headline>Transaction in Own Shares</tx_ann_headline> <nm_comp>Marks & Spencer Grp</nm_comp> <nm_long_comp>Marks & Spencer Group PLC</nm_long_comp> <nm_cont_long/> <tx_ann/> <ht_ann> <![CDATA[ <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=us-ascii" /> <title></title> <meta name="generator" content="RNS" /> <style type="text/css"> /**/ H1 { MARGIN: 24pt 0cm 0pt; FONT-FAMILY: "Cambria","serif"; COLOR: #365f91; FONT ]]> <![CDATA[ -SIZE: 14pt } H2 { MARGIN: 10pt 0cm 0pt; FONT-FAMILY: "Cambria","serif"; COLOR: #4f81bd; FONT-SIZE: 13pt } H3 { MARGIN: 10pt 0cm 0pt; FONT-FAMILY: "Cambria","serif"; COLOR: #4f81bd; FONT-SIZE: 12pt } H4 { FONT-STYLE: italic; MARGIN: 10pt 0cm 0pt; FONT-FAMILY: "Cambria","serif ]]> <![CDATA[ "; COLOR: #4f81bd; FONT-SIZE: 12pt } H5 { MARGIN: 10pt 0cm 0pt; FONT-FAMILY: "Cambria","serif"; COLOR: #243f60; FONT-SIZE: 12pt; FONT-WEIGHT: normal } H6 { FONT-STYLE: italic; MARGIN: 10pt 0cm 0pt; FONT-FAMILY: "Cambria","serif"; COLOR: #243f60; FONT-SIZE: 12pt; FONT-WEIGHT: normal } ]]> <![CDATA[ STRONG { FONT-FAMILY: "Times New Roman","serif" } EM { FONT-FAMILY: "Times New Roman","serif" } OL { MARGIN-BOTTOM: 0cm } UL { MARGIN-BOTTOM: 0cm } LINK { COLOR: blue } visited { COLOR: purple } BODY.ca { TEXT-JUSTIFY-TRIM: punctuation } .bz { ]]> <![CDATA[ MARGIN: 72pt 90pt; size: 612.0pt 792.0pt } DIV.bz { } P.cn { MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Times New Roman","serif"; FONT-SIZE: 12pt; FONT-WEIGHT: bold } SPAN.cl { FONT-FAMILY: "Arial","sans-serif"; FONT-SIZE: 11pt; FONT-WEIGHT: bold } Pa { MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Times New Roman","serif"; FONT-SIZE: 12pt } LI.a { MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Times New Roman","serif"; FONT-SIZE: 12pt } DIV.a { MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Times New Roman","serif"; FONT-SIZE: 12pt } SPAN.cj { FONT-FAMILY: "Arial","sans-serif"; FONT-SIZE: 11pt } P.co { TEXT-ALIGN: justify; MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Times New Roman","serif"; FONT-SIZE: 12pt } SPAN.ci { FONT-FAMILY: "Arial","sans-serif"; FONT-SIZE: 11pt } SPAN.cp { FONT-FAMILY: "Arial","sans-serif"; COLOR: windowtext; FONT-SIZE: 11pt } P.cq { TEXT-ALIGN: justify; MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Times New Roman","serif"; FONT-SIZE: 12pt; FONT-WEIGHT: bold } SPAN.cf { FONT-FAMILY: "Arial","sans-serif"; FONT-SIZE: 11pt; FONT-WEIGHT: bold } P.cr { TEXT-ALIGN: center; MARGIN: 0cm 0cm 0pt; FONT-FAMILY: "Times New Roman","serif"; FONT-SIZE: 12pt; FONT-WEIGHT: bold } SPAN.cd { FONT-FAMILY: "Arial","sans-serif"; FONT-SIZE: 11pt; FONT-WEIGHT: bold } SPAN.cb { FONT-WEIGHT: bold } /**/ </style> </head> <body class="ca"><div>RNS Number : 4614Y</div> <div>Marks & Spencer Group PLC</div> <div>08 September 2015</div> <div> </div> <div class="bz"> <p class="cn"><span class="cl">Marks & Spencer Group PLC</span></p> <p class="cn"><span class="cl"> </span></p> <p class="cn"><span class="cl">TRANSACTIONS IN OWN SECURITIES</span></p> <p class="a"><span class="cj"> </span></p> <p class="a"><span class="cj"> </span></p> <p class="a"><span class="cj"> </span></p> <p class="co"><span class="ci">Marks and Spencer Group plc</span><span class="cj"> (</span><span class="cp">"</span><span class="ci">Marks & Spencer</span><span class="cj">") announces today that it has purchased the following number of its ordinary shares of 25 pence each on the London Stock Exchange from </span><span class="ci">Morgan Stanley & Co. International PLC. Such purchase was effected pursuant to irrevocable instructions issued by </span><span class="ci">Marks & Spencer </span><span class="ci">on 7 July 2015, as announced on 7 July 2015</span><span class="ci">:</span></p> <p class="co"><span class="ci"> </span></p> <p class="cq"><span class="cf">Ordinary Shares</span></p> <p class="co"><span class="ci"> </span></p> <p class="co"><span class="ci">Date of purchase:                                           8</span><span class="ci"> September 2015</span></p> <p class="co"><span class="ci"> </span></p> <p class="co"><span class="ci">Number of ordinary shares purchased:          139,705</span></p> <p class="co"><span class="ci"> </span></p> <p class="co"><span class="ci">Price per share:                                              508.89p</span></p> <p class="co"><span class="ci"> </span></p> <p class="co"><span class="ci">Marks & Spencer </span><span class="ci">intends to cancel the purchased shares.</span></p> <p class="co"><span class="ci"> </span></p> <p class="co"><span class="ci">Since </span><span class="ci">8 July 2015</span><span class="cj">, </span><span class="ci">Marks & Spencer </span><span class="ci">has purchased 5,821,474 shares at a cost (including dealing and associated costs) of £30,696,909.</span></p> <p class="co"><span class="ci"> </span></p> <p class="cq"><span class="cf">This announcement does not constitute, or form part of, an offer or any solicitation of an offer for securities in any jurisdiction.</span></p> <p class="cr"><span class="cd"> </span></p> <p class="a"><span class="cj">Contact:<span style="FONT-WEIGHT: bold"><span class="cb"> </span></span></span></p> <p class="a"><span class="cj"> </span></p> <p class="a"><span class="cj">Anthony Clarke </span></p> <p class="a"><span class="cj">Deputy Secretary </span></p> <p class="a"><span class="cj">Marks and Spencer Group plc</span></p> <p class="a"><span class="cj">020 8718 9940 </span></p> <p class="cn"><span class="cl"> </span></p> </div> <div style="text-align:center;"><br/>This information is provided by RNS</div><div style="text-align:center;">The company news service from the London Stock Exchange</div><div> </div> END<div> </div><div> </div>POSLLFVRAFITIIE</body> </html> ]]> </ht_ann> <tx_webcast_url nm_prov=""/> </Announcement>
I'm accessing this like: 我正在访问这个:
foreach($this->xml->Announcement as $announcement) {
$this->createPDF($announcement->ht_ann);
}
Fixed it by writing the HTML to file before creating the pdf: 通过在创建pdf之前将HTML写入文件来修复它:
So to get pdfcrowd to print a variable $htmlString
I can do: 因此要获得pdfcrowd来打印变量
$htmlString
我可以这样做:
file_put_contents('xmlrequest.html', $htmlString);
$this->createPDF('xmlrequest.html');
I also updated my createPDF() function to use: 我还更新了我的createPDF()函数使用:
$client->convertFile($htmlContent, $out_file);
Instead of: 代替:
$client->convertHTML($htmlContent, $out_file);
I couldn't find any reason why this works and the way I was trying didn't. 我找不到任何理由为什么这样做,我尝试的方式没有。
I have faced the same issue, I resolved by following below steps: 我遇到了同样的问题,我按照以下步骤解决了这个问题:
$content = "<html><body></body></html"; //What ever html you want to send.
$pdf = $client->convertHtml("{$content}", $fd);
Thanks. 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.