簡體   English   中英

需要將html頁面下載為PDF

[英]Need to download the html page as PDF

我在HTML頁面中有一些交叉表,我想下載PDF格式的交叉表。 我已經在SAS報告標准中生成了相同的報告,但是由於PDF和HTML在SAS中無法相互呈現,因此嘗試開發JavaScript代碼段,以便可以將其嵌入SAS中以轉換HTML內容轉換為PDF。

我已經使用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> 

我嘗試在聯機Javascript代碼段中運行上述代碼,盡管沒有給出錯誤,但是單擊“生成PDF”按鈕並沒有將頁面下載為PDF格式。

有人可以告訴我我在想什么嗎?

也許是最簡單的情況。 使用SAS將相同的過程輸出同時發送到html和pdf目標。 每個目的地將創建一個單獨的文檔。 在html文檔中,添加指向pdf文檔的鏈接。 當兩個文件均作為靜態內容提供時,它們應位於相同的服務文件夾中。

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;

當瀏覽器打開時,單擊鏈接以查看pdf版本。

如果內容是通過提示和存儲過程在SAS中動態生成的,則為問題添加更多詳細信息。

您還可以擁有一個母版頁,以在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>

在研究任何更復雜的方法之前,您需要從輸出使用者那里獲得更清晰的規格。

 %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輸出圖像格式需要與PDF相同

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM