简体   繁体   English

需要将html页面下载为PDF

[英]Need to download the html page as PDF

I have some cross-tabs in HTML page and I want to download those in PDF format. 我在HTML页面中有一些交叉表,我想下载PDF格式的交叉表。 I have generated the same report in SAS reporting standard, but as the PDF and HTML doesn't render one another in SAS, so trying to develop a JavaScript piece of code, so that I can embed the same in SAS in order to convert the HTML contents into PDF. 我已经在SAS报告标准中生成了相同的报告,但是由于PDF和HTML在SAS中无法相互呈现,因此尝试开发JavaScript代码段,以便可以将其嵌入SAS中以转换HTML内容转换为PDF。

I have developed the below scripts in JavaScript, but I am not a expert in JavaScript. 我已经使用JavaScript开发了以下脚本,但是我不是JavaScript专家。

 var options = { "url": "/pdf/generate", "data": "data=" + $("#content").html(), "type": "post", } $.ajax(options) $html = $_POST['data']; $pdf = html2pdf($html); header("Content-Type: application/pdf"); //check this is the proper header for pdf header("Content-Disposition: attachment; filename='some.pdf';"); echo $pdf; $(function() { var specialElementHandlers = { '#editor': function(element, renderer) { return true; } }; $('#cmd').click(function() { var doc = new jsPDF(); doc.fromHTML( $('#target').html(), 15, 15, { 'width': 170, 'elementHandlers': specialElementHandlers }, function() { doc.save('sample-file.pdf'); } ); }); }); 
 <title>HTML TO PDF Rendering</title> <div id="content"> <h3>Hello, this is a H3 tag</h3> <p>A paragraph</p> </div> <div id="editor"></div> <button id="cmd">generate PDF</button> 

I tried to run the above piece of code in online Javascript snippet, though it's not giving an error, but clicking on the Generate PDF button is not downloading the page into PDF format. 我尝试在联机Javascript代码段中运行上述代码,尽管没有给出错误,但是单击“生成PDF”按钮并没有将页面下载为PDF格式。

Can someone please tell me what I am missing? 有人可以告诉我我在想什么吗?

Simplest case perhaps. 也许是最简单的情况。 Use SAS to send the same procedural output simultaneously to both html and pdf destinations. 使用SAS将相同的过程输出同时发送到html和pdf目标。 Each destination will create a separate document. 每个目的地将创建一个单独的文档。 In the html document add a link to the pdf document. 在html文档中,添加指向pdf文档的链接。 When both files are served as static content they should reside in the same served folder. 当两个文件均作为静态内容提供时,它们应位于相同的服务文件夹中。

ods html file = 'c:\temp\xtab.html' style=journal;
ods pdf file = 'c:\temp\xtab.pdf' style=journal;

ods noresults;

options nodate nonumber nocenter;

proc tabulate data=sashelp.cars;
  class make model type;
  table make, type=''*N='';
  where make like 'M%';
run;

ods pdf close;

ods html text='<a href="xtab.pdf">Link goes here</a>';

ods html close;

ods results;

options noxmin noxwait noxsync;
%sysexec start "My Browser" c:\temp\xtab.html;

When your browser opens, click on the link to see the pdf version. 当浏览器打开时,单击链接以查看pdf版本。

Add more details to the questions if the content is generated dynamically in SAS by way of a prompt and stored process. 如果内容是通过提示和存储过程在SAS中动态生成的,则为问题添加更多详细信息。

You could also have a master page that displays the differing content in an iframe 您还可以拥有一个母版页,以在iframe中显示不同的内容

<html>
<title>demo</title>
<body>
<button id="asHtml" onclick="asHtml()"> Show HTML output </button>
<button id="asPDF"  onclick="asPDF()"> Show PDF output </button>
<iframe id="theTabulate" width="100%" height="90%">
</iframe>
<script>
function asHtml() {
  document.getElementById("theTabulate").src = "xtab.html";
}
function asPDF() {
  document.getElementById("theTabulate").src = "xtab.pdf";
}
</script>
</body>
</html>

You would need much clearer specifications from your output consumer before delving into any more sophisticated. 在研究任何更复杂的方法之前,您需要从输出使用者那里获得更清晰的规格。

 %macro mac_buttons_ods( mcv_style = ERS, /* Select the SAS style you want to use. default = ers */ mcv_dest = HTML, /* PDF, RTF, XML, EXCEL, PANEL, SPECIAL, TEXT or TABLE */ mcv_tag_options = , mcv_excel_options = %str(options(sheet_label="ERS" sheet_interval="none" frozen_headers="yes" embedded_titles="yes")), mcv_ods_options = %str(), mcv_title = ERS, /* Enter the text you want to see in the title area of the browser window.*/ mcv_portal_only = no, mcv_mode = PROD, /* PROD: mprint mlogic symbolgen will be turned off otherwise they will be turned on. */ mcv_param =,/*all the parameters to pass in url*/ ) ; %global mcv_stp_name mcv_ers_url mcv_sasweb _odsdest _odsoptions _odsstyle _odsstylesheet _program _url mcv_filename mcv_back mcv_forward mcv_pdf mcv_excel mcv_word mcv_print mcv_rerun mcv_close ; %local mcv_style mcv_dest mcv_tag_options mcv_excel_options mcv_title mcv_ods_options mcv_portal_only mcv_mode ; *ProcessBody ; options minoperator ; %* Only use this macro if running as a stored process ; %if %superq(_program) = %then %return ;; libname ersstyle '/apps/usr/ERS/styles' access=readonly ; ods path ersstyle.templat(read) sasuser.templat(update) sashelp.tmplmst(read) ; ods escapechar = '^' ; %let _odsstyle = &mcv_style. ; %let mcv_sasweb = https://&_srvname.:&_srvport./ ; data _null_ ; call symputx('mcv_stp_name',scanq("&_program.",countc("&_program.","/"),'/')) ; run ; %mac_assign_lib; %let mcv_back = "<button class='NAV' onclick=history.go(-1)%str(;) title='Go Back 1 Page'><img src='/images/left.png' /></button>" ; %let mcv_forward ="<button class='NAV' onclick=history.go(+1)%str(;) title='Go Forward 1 Page'><img src='/images/right.png' /></button>"; %let mcv_pdf = "<button class='NAV' onclick=location.href='do?_program=%sysfunc(urlencode(&_program.))&mcv_param.%str(&)_odsdest=PDF' title='Click to Download to PDF.' ><img src='/images/pdf.png' /></button>"; %let mcv_excel = "<button class='NAV' onclick=location.href='do?_program=%sysfunc(urlencode(&_program.))&mcv_param.%str(&)_odsdest=EXCEL' title='Click to Download to Excel.'><img src='/images/excel_small.png' /></button>"; %let mcv_word = "<button class='NAV' onclick=location.href='do?_program=%sysfunc(urlencode(&_program.))&mcv_param.%str(&)_odsdest=RTF' title='Click to Download to Word'><img src='/images/word.png' /></button>"; %let mcv_print = "<button class='NAV' onclick=window.print(); title='Click to Print report.'><img src='/images/print.png' /></button>"; %let mcv_rerun = "<button class='NAV' onclick=window.location.reload(); title='Click to Re-Run report.'> <img src='/images/rerun.png' /></button>"; %let mcv_close = "<button class='NAV' onclick=self.close(); title='Click to Close window.'><img src='/images/close.png' /></button>"; %if %upcase(%superq(mcv_portal_only)) = YES and %superq(_result) = PACKAGE_TO_ARCHIVE %then %do ; %stpbegin ; ods text = "^{style [fontsize=14pt]&c_stp_name.}" ; ods text = "^{style [fontsize=14pt]Can only be viewed on the ERS Portal.}" ; %end ; %else %do ; %if %upcase(%superq(_odsdest)) ne HTML and %upcase(%superq(_odsdest)) > %str() %then %let mcv_dest = &_odsdest. ;; %if %upcase(%superq(mcv_dest)) = EXCEL %then %do ; data _null_ ; rc = stpsrv_header('Content-type','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') ; rc = stpsrv_header('Content-disposition',"attachment; filename=&mcv_stp_name..xlsx") ; run ; ods excel file = _webout ; ods excel style = &mcv_style. %if %superq(mcv_excel_options) > %str() %then &mcv_excel_options. ;; %end ; %else %let _odsdest = &mcv_dest. ;; %if %upcase(%superq(mcv_dest)) = PANEL %then %let _odsdest = tagsets.htmlpanel ; %if %upcase(%superq(mcv_dest)) = TABLE %then %let _odsdest = tagsets.tableeditor ; %if %upcase(%superq(mcv_dest)) = SPECIAL %then %let _odsdest = tagsets.special ; %if %upcase(%superq(mcv_dest)) =HTML OR %upcase(%superq(mcv_dest))=PANEL OR %upcase(%superq(mcv_dest))=TABLE OR %upcase(%superq(mcv_dest))=SPECIAL %then %do ; %include "/apps/usr/ERS/MacroLibrary/tableeditor.tpl"; /* ods tagsets.tableeditor file=_webout style= &mcv_tag_options.;*/ %let _odsoptions = file=_webout (title='ERS') style= &mcv_style. &mcv_tag_options. ; %end ; %if %upcase(%superq(mcv_dest)) = PDF %then %do ; %let _odsoptions = notoc &mcv_ods_options. ; data _null_ ; rc = stpsrv_header('Content-disposition',"attachment; filename=&mcv_stp_name..pdf") ; run ; %end ; %if %upcase(%superq(mcv_dest)) = RTF %then %do ; %let _ODSOPTIONS= KEEPN; data _null_ ; rc = stpsrv_header('Content-disposition',"attachment; filename=&mcv_stp_name..doc") ; run ; %end ; %if %upcase(%superq(mcv_dest)) ne EXCEL %then %stpbegin ;; %end ; %if %upcase(&mcv_mode) ne PROD %then options mlogic mprint symbolgen ; %else options nomlogic nomprint nosymbolgen ;; %mend mac_buttons_ods; 

Excel Output Image format the same is needed in PDF Excel输出图像格式需要与PDF相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM