【发布时间】:2018-06-06 00:57:52
【问题描述】:
我创建了一个 html 文件,我将使用它来使用 SelectPDF 生成 PDF 表单。这个 HTML 有文本字段,需要用我的 MySQL 数据库中的数据填充/检查的复选框。我想使用c# 与我的数据库进行通信,所以我使用的是实体框架。这个想法是让控制台应用程序运行,从数据库中获取信息,填充 html 文件中的字段,然后程序从中生成 PDF。到目前为止,HTML 到 PDF 部分按预期工作。
我不知道如何将数据库中的表中的数据传递到我的简单 HTML 文件,因为我没有使用 Razor 页面/ASPX 文件。
我如何使用我的实体框架模型来解决这个问题?
目前,我正在使用一个包含 5 个表的小型数据库进行测试 - 真正的数据库有 20 多个表。
Program.cs 代码:
//assign html file to string, add directory location of file.
string HtmlString = (@"C:\Users\UserName\Documents\Visual Studio 2017\ConsoleTest\ConsoleTest\IIAForm30.html");
// instantiate a html to pdf converter object
HtmlToPdf converter = new HtmlToPdf();
/*********************************************************
* *
* *
* PDF Settings *
* *
* *
* *******************************************************/
//set PDF page size
//supports:(A0 to A10),(B0 to B5),(ArchA to ArchE),(Letter, HalfLetter, Letter11x17, Note, Flsa, Ledger)
converter.Options.PdfPageSize = PdfPageSize.A4;
//set PDF page orientation
//Supports Landscape and Portrait
converter.Options.PdfPageOrientation = PdfPageOrientation.Portrait;
//ShrinkOnly: the html content is resized only if the content width is
//larger than the destination space (pdf page or rectangle) width.
converter.Options.AutoFitWidth = HtmlToPdfPageFitMode.ShrinkOnly;
// NoAdjustment: the html content is not resized vertically in any way to fit the available space.
//If the content is larger, it will be cut and not all of it will be displayed in the generated pdf file.
converter.Options.AutoFitHeight = HtmlToPdfPageFitMode.NoAdjustment;
// header settings
converter.Options.DisplayHeader = true;
converter.Header.DisplayOnFirstPage = false;
converter.Header.DisplayOnOddPages = true;
converter.Header.DisplayOnEvenPages = true;
converter.Header.Height = 20;
//Page Layout
//SinglePage: Displays one page at a time when open.
converter.Options.ViewerPreferences.PageLayout = PdfViewerPageLayout.SinglePage;
//converts html to pdf.
PdfDocument doc1 = converter.ConvertUrl(HtmlString);
PdfDocument doc2 = converter.ConvertUrl(HtmlString);
PdfDocument doc3 = converter.ConvertUrl(HtmlString);
// save pdf document
doc1.Save("Sample1.pdf");
doc2.Save("Sample2.pdf");
doc3.Save("Sample3.pdf");
// create a new pdf document
PdfDocument doc = new PdfDocument();
// add the pages of these documents to the new document
doc.Append(doc1);
doc.Append(doc2);
doc.Append(doc3);
// save pdf document
doc.Save("MergedDoc.pdf");
// close pdf document
doc.Close();
// close the original pdf documents
doc1.Close();
doc2.Close();
doc3.Close();
//original pdf files are discarded/deleted
File.Delete("Sample1.pdf");
File.Delete("Sample2.pdf");
File.Delete("Sample3.pdf");
HTML sn-p for context(仅使用纯文本占据数据库日期所在的位置):
<table class="tg-3" style="undefined;table-layout: fixed; width: 100%">
<colgroup>
<col style="width: 130px">
<col style="width: 120px">
<col style="width: 120px">
<col style="width: 110px">
<col style="width: 100px">
<col style="width: 100px">
<col style="width: 150px">
</colgroup>
<tr>
<td class="tg-576q" colspan="7" style="border-top: none;">I. Inspector Information</td>
</tr>
<tr>
<td class="tg-r0wx" style="border-bottom: none;">Date of Inspection</td>
<td class="tg-es7u" style="border-bottom: none;border-right: none;">Inspected by:</td>
<td class="tg-9vem" rowspan="3" style="border-right: none;border-left: none;border-right: none;">John Doe</td>
<td class="tg-r0wx" style="border-bottom: none; border-right: none;border-left: none;"></td>
<td class="tg-9vem" rowspan="3" style="border-left: none; border-right: none;">Inspector</td>
<td class="tg-es7u" style="border-left: none;border-right: none;border-bottom: none;"></td>
<td class="tg-9vem" rowspan="3" style="border-left: none;">(777)777-7777</td>
</tr>
<tr>
<td class="tg-9vem" rowspan="1" style="border-top: none; border-right-style: solid;border-bottom: none; text-align: right">2018-02-03</td>
<td class="tg-es7u" rowspan="2" style="border-right: none;border-top: none; border-left-style: solid;">Name:</td>
<td class="tg-es7u" rowspan="2" style="border-top: none; border-right: none; border-left: none;">Title:</td>
<td class="tg-es7u" rowspan="2" style="border-left: none; border-top: none; border-right: none;">Phone Number:</td>
</tr>
<tr>
<td rowspan="1" style="border-top: none"></td>
</tr>
<tr>
<td class="tg-r0wx" colspan="3" style="border-bottom: none;">Inspection Company</td>
<td class="tg-es7u" colspan="1" rowspan="3" style="border-right: none;">Inspector Signature:</td>
<td class="tg-n603" colspan="3" rowspan="3" style="border-left: none;"><img src="signatureStock.PNG" width="180" height="50"></td>
</tr>
<tr>
<td class="tg-tnbn" colspan="3" rowspan="1" style="border-top: none;">John Doe Company</td>
</tr>
</table>
【问题讨论】:
-
为了获得有关堆栈溢出问题的最佳答案,通常最好尝试将您的问题压缩为尽可能小的损坏代码。开放式问题或大型问题空间通常不会引起太多关注。
-
老实说,我不知道我怎么能以更具体的方式提出这个问题。但你说的是真的,我会在以后发布任何问题时记住这一点,谢谢。
标签: c# html mysql entity-framework-6