0
在我的应用程序中,首先允许用户使用CKEDITOR创建html文档,其中用户可以创建html文档,并且可以插入图像,表单字段等,而不是转换生成的HTML文档进入PDF。 如果HTML文档包含纯文本而不是PDF文件被成功创建,但是如果用户在其中插入图像而不是提供错误。 创建PDF文档的代码。使用itextsharp将图像转换为PDF时出错
public ActionResult CreateFile(FormCollection data)
{
var filename = data["filename"];
var htmlContent = data["content"];
string sFilePath = Server.MapPath(_createdPDF + filename + ".html");
htmlContent = htmlContent.Trim();
if (!System.IO.File.Exists(sFilePath))
{
using (FileStream fs = new FileStream(sFilePath, FileMode.Create))
{
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
w.Write(htmlContent);
}
}
createPDF(sFilePath);
}
return View();
}
private MemoryStream createPDF(string sFilePath)
{
string filename = Path.GetFileNameWithoutExtension(sFilePath);
string name = Server.MapPath(_createdPDF + filename + ".pdf");
MemoryStream ms = new MemoryStream();
TextReader tr = new StringReader(sFilePath);
Document document = new Document(PageSize.A4, 30, 30, 30, 30);
string urldir = Request.Url.GetLeftPart(UriPartial.Path);
urldir = urldir.Substring(0, urldir.LastIndexOf("/") + 1);
Response.Write(urldir);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(name, FileMode.Create));
document.Open();
string htmlText = "";
StreamReader sr;
sr = System.IO.File.OpenText(sFilePath);
htmlText = sr.ReadToEnd();
sr.Close();
WebClient wc = new WebClient();
Response.Write(htmlText);
var props = new Dictionary<string, Object>();
props["img_baseurl"] = @"C:\Documents and Settings\shubham\My Documents\visdatemplatemanger\visdatemplatemanger\";
List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(htmlText), null,props);
for (int k = 0; k < htmlarraylist.Count; k++)
{
document.Add((IElement)htmlarraylist[k]);
}
document.Close();
System.IO.File.Delete(sFilePath);
UploadURL(name);
return ms;
}
,我得到的,如果图像是包括HTML文档中的错误是:
Could not find a part of the path 'C:\Program Files\Common Files\Microsoft Shared\PDFimages\rectangle-shape.png'.
在这个应用程序允许用户上传图像。路径可以是任何东西。 我想要的是将HTML文件(可以包含图像,表格,表单域等)转换为PDF文档。 如果用户创建仅包含文本而不包含任何图像的文件,上面的代码工作正常。请为此提供解决方案 – pihu
如果用户可以上传图像,则只需将图像存储在服务器上的文件夹中,并将上面的代码指向该文件夹即可。 –
我照你所说的做了,但仍然得到错误信息:“找不到路径的一部分”C:\ Documents and Settings \ shubham \ My Documents \ visdatemplatemanger \ PDFimages \ rectangle-shape.png'。“我允许用户在PDF中包含的所有图像都存储在C:\ Documents and Settings \ shubham \ My Documents \ visdatemplatemanger \ PDFimages \ – pihu