How to do it...

  1. Add an aspx web page that contains invoice details. I have simply taken from the example on DocRaptor's site and modified it slightly. Call this page InvoicePrint.aspx.
I have included the CSS in a style sheet called invoice.css. Be sure to get this from the source code accompanying this book.

There are a few ways you could approach this code. This is not necessarily the only way to create the web pages. If you are using .NET Core MVC, your approach would probably be different. However if you do this, remember that this code is just to illustrate the concept here.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="InvoicePrint.aspx.cs" Inherits="Serverless.InvoicePrint" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Invoice</title>
<meta http-equiv="content-type" content="text/html;
charset=utf-8"/>
<link href="css/invoice.css" rel="stylesheet" />
<script type="text/javascript">
function ToggleErrorDisplay()
{
if ($("#errorDetails").is(":visible")) {
$("#errorDetails").hide();
} else {
$("#errorDetails").show();
}
}

function TogglePrintResult() {
if ($("#printDetails").is(":visible")) {
$("#printDetails").hide();
} else {
$("#printDetails").show();
}
}
</script>
</head>
<body>
<form runat="server">
<div id="container">
<div id="main">
<div id="header">
<div id="header_info black">The Software Company
<span class="black">|</span> (072)-412-5920
<span class="black">|</span> software.com</div>
</div>
<h1 class="black" id="quote_name">Invoice INV00015</h1>
<div id="client" style="float: right">
<div id="client_header">client:</div>
<p class="address black">
Mr. Wyle E. Coyote
</p>
</div>
<table id="phase_details">
<thead>
<tr>
<th class="title">Stock Code</th>
<th class="description">Item Description</th>
<th class="price">price</th>
</tr>
</thead>
<tr class="first black">
<td>BCR902I45</td>
<td>Acme Company Roadrunner Catch'em Kit</td>
<td class="price">
<div class="price_container">$300</div>
</td>
</tr>
<tr>
<td></td>
<td>Booster Skates</td>
<td class="price">
<div class="price_container">$200</div>
</td>
</tr>
<tr>
<td></td>
<td>Emergency Parachute</td>
<td class="price">
<div class="price_container">$100</div>
</td>
</tr>
<tr class="last">
<td></td>
<td></td>
<td></td>
</tr>
<tr class="first black">
<td>BFT547J78</td>
<td>Very Sneaky Trick Seed Kit</td>
<td class="price">
<div class="price_container">$800</div>
</td>
</tr>
<tr>
<td></td>
<td>Giant Magnet and Lead Roadrunner Seeds</td>
<td class="price">
<div class="price_container">$500</div>
</td>
</tr>
<tr>
<td></td>
<td>Rollerblades</td>
<td class="price">
<div class="price_container">$300</div>
</td>
</tr>
<tr class="last">
<td></td>
<td></td>
<td></td>
</tr>
</table>
</div>
<div id="total_price">
<h2>TOTAL: <span class="price black">$1100</span></h2>
</div>
<div id="print_link">
<asp:LinkButton ID="lnkPrintInvoice" runat="server"
Text="Print this invoice" OnClick="lnkPrintInvoice_Click">
</asp:LinkButton>
</div>
<div id="errorDetails">
<asp:Label ID="lblErrorDetails" runat="server">
</asp:Label>
</div>
<div id="printDetails">
<asp:Label ID="lblPrintDetails" runat="server">
</asp:Label>
</div>
</div>
</form>

</body>
</html>
  1. I also created a print friendly version of the invoice page called invoice.html.
  2. The next step is to create a click event for the link button. Add the following code to the click event. You will notice that I have just hard coded the path to generate the PDF document to as: C:tempinvoiceDownloads. You need to make sure that you change this path if you want to output to a different path (or get a path relative to the server you're on).
        Configuration.Default.Username = "YOUR_API_KEY_HERE";
DocApi docraptor = new DocApi();

Doc doc = new Doc(
Test: true,
Name: "docraptor-csharp.pdf",
DocumentType: Doc.DocumentTypeEnum.Pdf,
DocumentContent: GetInvoiceContent()
);

byte[] create_response = docraptor.CreateDoc(doc);
File.WriteAllBytes(@"C:tempinvoiceDownloadsinvoice.pdf",
create_response);
  1. Make sure that you include the following namespaces in your web page:
        using System;
using System.Web.UI;
using DocRaptor.Client;
using DocRaptor.Model;
using DocRaptor.Api;
using System.IO;
using System.Net;
using System.Text;
  1. Lastly, get the HTML content of the print friendly page called invoice.html. The URL in the code below will differ on your machine because your port number will probably be different.
        private string GetInvoiceContent()
{
WebRequest req = WebRequest.Create
("http://localhost:37464/invoice.html");
WebResponse resp = req.GetResponse();
Stream st = resp.GetResponseStream();
StreamReader sr = new StreamReader(st, Encoding.ASCII);
return sr.ReadToEnd();
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.18.141