MVC 애플리케이션이 있고 HTML을 사용하여 PDF를 생성하려고합니다. 여기에서는 wkHTMLtoPdf exe를 사용하여 내 작업을 수행합니다.wkHtmlToPdf PDF를 생성 할 때 CSS가 적용되지 않습니다.
jquery를 사용하여 클라이언트 측에서 조치 메소드를 호출합니다. 그런 다음 방법 아래로 쳤다.
public FileContentResult ExportInvoiceASPDF(string invno)
{
try
{
Byte[] bytes;
var fullUrl = this.Url.Action("pdfCustomerInvoice", "Report", new { invNo = invno }, Request.Url.Scheme);
bytes = WKHtmlToPdf(fullUrl);
FileContentResult result = new FileContentResult(bytes, "application/pdf")
{
FileDownloadName = "PriceNotification"
};
return File(result.FileContents, "application/pdf");
}
catch (Exception ex)
{
throw;
}
}
여기서 바이트 스트림을 가져 오는 WKHtmlToPdf 함수를 호출했습니다. (아래 코드는 stackoverflow 스레드에서 추출한)
public byte[] WKHtmlToPdf(string url_input)
{
try
{
var fileName = " - ";
var wkhtmlDir = ConfigurationSettings.AppSettings["wkhtmlDir"];//Directory of wkHtmltopdf exe
var wkhtml = ConfigurationSettings.AppSettings["wkhtml"];//wkhtmltopdf exe location
var p = new Process();
url = url_input;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = wkhtml;
p.StartInfo.WorkingDirectory = wkhtmlDir;
string switches = "";
switches += "--print-media-type ";
switches += "--margin-top 10mm --margin-bottom 10mm --margin-right 10mm --margin-left 10mm ";
switches += "--page-size Letter ";
p.StartInfo.Arguments = switches + " " + url + " " + fileName;
p.Start();
//read output
byte[] buffer = new byte[32768];
byte[] file;
using (var ms = new MemoryStream())
{
while (true)
{
int read = p.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length);
if (read <= 0)
{
break;
}
ms.Write(buffer, 0, read);
}
file = ms.ToArray();
}
// wait or exit
p.WaitForExit(60000);
// read the exit code, close process
int returnCode = p.ExitCode;
p.Close();
return returnCode == 0 ? file : null;
}
catch (Exception ex)
{
// set your exceptions here
return null;
}
}
여기에서보기를 실행합니다. (PDF로 내보낼 페이지)
public ActionResult pdfCustomerInvoice(string invNo)
{
var inv = _customerInvoiceService.GetCustomerInvoice(invNo);
InvoiceSummaryReportsVM invRepVM = new InvoiceSummaryReportsVM();
//My data assigning part going here
return View(invRepVM);
}
다음은 pdfCustomerInvoice html입니다.
@model Pro.Web.Models.ViewModels.InvoiceSummaryReportsVM
@{
Layout = null;
}
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="center">
<h2>Tax Invoice</h2>
</div>
<div class="row">
<br />
<br />
<br />
<br />
<br />
</div>
<div class="row">
<div class="col-md-6">
</div>
<div class="col-md-6">
<div class="row">
<div class="col-md-3">
@Html.LabelFor(model => model.InvoiceNo, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-3">
@Html.DisplayFor(model => model.InvoiceNo, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="row">
<div class="col-md-3">
@Html.LabelFor(model => model.InvoiceDate, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-3">
@Html.DisplayFor(model => model.InvoiceDate, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="row">
<div class="col-md-3">
@Html.LabelFor(model => model.AccountNo, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-3">
@Html.DisplayFor(model => model.AccountNo, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="row">
<div class="col-md-3">
@Html.LabelFor(model => model.InvoiceStatementPeriod, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-3">
@Html.DisplayFor(model => model.InvoiceStatementPeriod, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
@Html.LabelFor(model => model.OpeningBalance, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-6">
@Html.DisplayFor(model => model.OpeningBalance, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="row">
<div class="col-md-6">
@Html.LabelFor(model => model.InvoiceTotal, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-6">
@Html.DisplayFor(model => model.InvoiceTotal, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="row">
<div class="col-md-6">
@Html.LabelFor(model => model.TaxAmount, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-6">
@Html.DisplayFor(model => model.TaxAmount, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="row">
<div class="col-md-6">
@Html.LabelFor(model => model.TotalAmountPayable, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-6">
@Html.DisplayFor(model => model.TotalAmountPayable, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
</body>
</html>
문제는 코드를 실행하면 스타일을 지정하지 않고 PDF가 생성된다는 것입니다. (모든 데이터가 왼쪽에 정렬됩니다.) 그러나 "pdfCustomerInvoice"메서드를 직접 호출하면 html이 스타일 지정과 함께 올바르게 작동합니다.
여기에서 나는 스타일링 문제가 있습니다. 이 문제를 해결할 방향을 알려주십시오.