Javascript 如何从其他链接创建pdf
我想从另一个网站生成pdf,因为这个html页面是不必要的项目。 我使用了jspdf、jquery.ajax库,我的代码如下所示:Javascript 如何从其他链接创建pdf,javascript,jquery,html,css,pdf,Javascript,Jquery,Html,Css,Pdf,我想从另一个网站生成pdf,因为这个html页面是不必要的项目。 我使用了jspdf、jquery.ajax库,我的代码如下所示: <script src='http://mrrio.github.io/jsPDF/dist/jspdf.debug.js'></script> <script type="text/javascript" src="http://cdn.rawgit.com/niklasvh/html2canvas/
<script src='http://mrrio.github.io/jsPDF/dist/jspdf.debug.js'></script>
<script type="text/javascript" src="http://cdn.rawgit.com/niklasvh/html2canvas/0.5.0-alpha2/dist/html2canvas.min.js"></script>
$('#create_pdf').on('click',function(){
var doc = new jsPDF('p','px','a4');
var source1 = '';
var
form = '', term='';
$.ajax({
type: "get",
url: "clients.php",
dataType: "html",async: false,
data: {
term: term
},
success: function( data ) {
source1= data ;
}
});
//alert($(source1).find('.container').eq(1).html());
doc.addHTML(source1,options,function() {
var string = doc.output('datauristring');
$('.preview-pane').attr('src', string);
});
});
$('create#u pdf')。在('click',function()上{
var doc=新的jsPDF('p'、'px'、'a4');
var source1='';
变量
形式='',术语='';
$.ajax({
键入:“获取”,
url:“clients.php”,
数据类型:“html”,异步:false,
数据:{
任期:任期
},
成功:功能(数据){
source1=数据;
}
});
//警报($(source1.find('.container').eq(1.html());
doc.addHTML(source1,options,function(){
var string=doc.output('datauristring');
$('.preview pane').attr('src',string);
});
});
它不工作,但警报给我html的积极结果
问题2。
如何生成PDF以使用不同的css文件(例如媒体=打印)在PDF中添加和删除项目并打印。html2canvas不使用media=print我使用PHP库。我加载HTML内容并对其进行解析,创建一个新的HTML代码(包括一个特殊的CSS),并使用html2pdf创建一个PDF
JS:
PHP(lib/html2pdf/pdf_engine.PHP):
PHP返回文件名,jQuery在新窗口中打开文件
$.ajax({
url: "lib/html2pdf/pdf_engine.php",
method: "post",
success: function(data) {
window.open( "documents/" + data );
},
error: function( error ) {
console.log( error );
}
});
<?php
date_default_timezone_set('Europe/Berlin');
// load the PDF class
require_once(dirname(__FILE__).'/html2pdf.class.php');
// get the print page
$data = file-get-contents("<your url>");
// remove line breaks
$data = preg_replace("/[\n\r]/", " ", $data);
// remove whitespaces
$data = preg_replace('/\s+/', ' ', $data);
// do whatever ...
// set the pdf content
$html = " <page backtop=\"15mm\" backbottom=\"15mm\">
<page_header>
<table style=\"border-bottom: 1px solid #000; width: 100%; padding-bottom: 10px;\">
<tr>
<td width=\"640\">Page Title</td>
</tr>
</table>
</page_header>
<page_footer>
<table style=\"border-top: 1px solid #000; width: 100%; padding-top: 10px;\"><tr>
<td width=\"320\">© " . date('Y') . " My Company</td>
<td width=\"320\" style=\"text-align: right;\">Seite [[page_cu]] of [[page_nb]]</td>
</tr></table>
</page_footer>
<link rel=\"stylesheet\" type=\"text/css\" href=\"style_pdf.css\">
" . $data . "
</page>";
// load the HTML2PDF class
$html2pdf = new HTML2PDF('P','A4','de', true, 'UTF-8', array(20, 10, 20, 10));
// create the document
$html2pdf->WriteHTML($html);
// send the document to the browser and force the download
$dir = "C:/path/to/documents/"; // remember to give access
$file_name = "some_funky_filename.pdf";
$html2pdf->Output($dir . $file_name, 'F');
echo $file_name;
?>