2018년 12월 26일 수요일

Classic ASP에서 HTML 데이터를 PDF로 만들어 보자

Classic ASP를 이용하여 HTML 데이터를 PDF로 만들어 보자 


테스트 환경은 WINDOWS 7, IIS7.5 입니다.

[준비물]
html2pdf 다운로드 : https://wkhtmltopdf.org/ 에서 OS에 맞는 SW를 다운로드한다.



1. IIS(인터넷 정보 서비스) 관리자를 실행하여 새로운 사이트를 하나 생성한다.

   생성할 디렉터리와 파일은 다음처럼 생성한다.

   Root-----html2pdf(folder)
                    |-----html2pdf.asp
                    |-----html2pdf_pr.asp
                    |-----convert(folder)

2. html2pdf.asp 를 만든다.
<html lang="en">
<head>  
  <title>html2pdf demo</pre>
<script src="/js/jquery.js" type="text/javascript">
  <script src="/js/jquery.form.js" type="text/javascript">

  <script type="text/javascript">
 
 function save() {
     var input_url_data = $('#input_url').val();
  $.post("html2pdf_pr.asp", {inputUrlData:input_url_data,submit:true})
  .done(
   function(data, textStatus, jqXHR){
    //alert(data);
    $('#pdf_file_name').val(data);
    alert("파일이 생성되었습니다.");
   }
   
  ).fail(
   function(jqXHR, textStatus, errorThrown){
    alert(textStatus);
    //alert("파일전송중 오류가 발생하였습니다.\n다시한번 시도해주세요.");
   }
   
  );
 }
 
 $( document ).ready(function() {
  var wrapper = document.getElementById("frm_1");
  var saveButton = wrapper.querySelector("[data-action=save]");
  saveButton.addEventListener("click", function (event) {
    if ($('#input_url').val() == "") {
   alert("Please provide a url first.");
    } else {
   save();
    }
  });
 });
 </script>
</head>
<body>
<div id="frm_1">
    url :
    <input id="input_url" name="input_url" type="text" /> 
    <button class="button save" data-action="save" type="button">SAVE  
    pdf file name :
    <input id="pdf_file_name" name="pdf_file_name" type="text" />
</div>
</body>
</html>

3. Html2pdf_pr.asp 내용을 만든다

<%@ Language=VBScript %>
<% Option Explicit %>
<%
On Error Resume Next 
dim PdfFileName
'response.write(request("inputUrlData"))
PdfFileName = GetTimeStamp() & ".pdf"

Function GetTimeStamp ()
 Dim dd, mm, yy, hh, nn, ss
 Dim datevalue, timevalue, dtsnow, dtsvalue

 'Store DateTimeStamp once.
 dtsnow = Now()

 'Individual date components
 dd = Right("00" & Day(dtsnow), 2)
 mm = Right("00" & Month(dtsnow), 2)
 yy = Year(dtsnow)
 hh = Right("00" & Hour(dtsnow), 2)
 nn = Right("00" & Minute(dtsnow), 2)
 ss = Right("00" & Second(dtsnow), 2)

 'Build the date string in the format yyyy-mm-dd
 datevalue = yy & "_" & mm & "_" & dd
 'Build the time string in the format hh:mm:ss
 timevalue = hh & "_" & nn & "_" & ss
 'Concatenate both together to build the timestamp yyyy-mm-dd hh:mm:ss
 dtsvalue = datevalue & "_" & timevalue
 GetTimeStamp = dtsvalue
End Function


Dim inputUrlData
Dim strCmd, xShell

inputUrlData = Request.Form("inputUrlData")

set xShell = CreateObject("WScript.Shell")
strCmd = Server.Mappath("/bin") & "\wkhtmltopdf.exe " _
   & inputUrlData & " " & Server.Mappath("/html2pdf") _ 
   & "\convert\" & PdfFileName

'response.write strCmd
'response.write "
"
call xShell.Run(strCmd, 1, true)


If Err Then 
 Response.Write "error"
 Response.Write Err.Description 
 Response.End 
Else
 response.write "/html2pdf/convert/" & PdfFileName 
End If 
%>

4. TEST해본다.

Fetch links Quick & Easy. Stream and Direct Download. Give it a try at http://seedr.cc now!

  Fetch links Quick & Easy. Stream and Direct Download. Give it a try at http:// seedr.cc now!   Fetch links Quick & Easy. Stream a...