需要将html页面下载为PDF

时间:2019-01-10 12:22:22

标签: javascript php ajax sas

我在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格式。

有人可以告诉我我在想什么吗?

2 个答案:

答案 0 :(得分:0)

最简单的情况。使用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中动态生成的,则为问题添加更多详细信息。

您还可以有一个 master 页面,该页面在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>

在研究任何更复杂的方法之前,您需要从输出使用者那里获得更清晰的规格。

答案 1 :(得分:0)

%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