Skip to content

Commit

Permalink
Improvements:
Browse files Browse the repository at this point in the history
- ASP.NET 64bit support for html2pdf.
- Attribute to suppress parallel execution.
- Ghostscript rendering.
- No need for a snapshot for a imagemap, you can use original svg image.
  • Loading branch information
simonegli committed Jul 15, 2014
1 parent c063329 commit 7f0199d
Show file tree
Hide file tree
Showing 95 changed files with 22,267 additions and 9,408 deletions.
116 changes: 60 additions & 56 deletions XamlImageConverter.Awesomium/Html2PDF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,60 @@

namespace XamlImageConverter {

public class Html2PDF: Snapshot.Html2PDFConverter {
public static class Html2PDF {

public ManualResetEventSlim SavedSignal = new ManualResetEventSlim(true);
public IWebView View = null;
public WebSession Session = null;
public static ManualResetEventSlim SavedSignal = new ManualResetEventSlim(true);
public static IWebView View = null;
public static WebSession Session = null;
public static bool AssemblyResolverInitialized = false;
public int Instances = 0;
public IDisposable FileLock = null;
public static int Instances = 0;
public static IDisposable FileLock = null;
public static object Lock = new object();

public void SaveAsync(Snapshot s) {
string path;
var source = ((Group.HtmlSource)s.Element).Source;
public static void SaveAsync(string source, string localfile, string elementname, string width, string height, string dpi) {
var path = Path.GetDirectoryName(localfile);
//var source = ((Group.HtmlSource)s.Element).Source;
string html = null;
if (source.StartsWith("http:https://") || source.StartsWith("https://")) {
try {
if (source.StartsWith("http:https://") || source.StartsWith("https://") || source.StartsWith("~")) {
/*try {
var ctx = HttpContext.Current;
var app = ctx.Request.ApplicationPath;
var root = ctx.Request.Url.Authority + "/" + app;
var oldpath = ctx.Request.Url.AbsolutePath;
path = null;
if (source.StartsWith(root)) {
var oldpath = ctx.Request.Url.AbsolutePath;
path = source.Substring(root.Length);
} else if (source.StartsWith("~")) {
path = source.Substring(1);
}
if (path != null) {
using (var w = new StringWriter()) {
ctx.RewritePath(path);
ctx.Server.Execute(path, w, false);
ctx.RewritePath(oldpath);
html = w.ToString();
}
}
} catch { }
/*if (html == null) {
var web = new WebClient();
web.Encoding = Encoding.UTF8;
html = web.DownloadString(source);
}*/
} else {
html = File.ReadAllText(s.Compiler.MapPath(source));
} catch {
} finally {*/
/* if (html == null) {
var web = new WebClient();
web.Encoding = Encoding.UTF8;
html = web.DownloadString(source);
}
//} */
}
var binpath = Path.GetDirectoryName(new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath);

//AppDomain.CurrentDomain.AppendPrivatePath("Lazy\\Awesomium");
if (!AssemblyResolverInitialized) {
int resolving = 0;
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => {
try {
resolving++;
var apath = s.Compiler.BinPath("Lazy\\Awesomium\\");
var apath = binpath;
var aname = new System.Reflection.AssemblyName(args.Name);
aname.CodeBase = apath + args.Name + ".dll";
aname.CodeBase = Path.Combine(apath, args.Name + ".dll");
return resolving > 1 ? null : System.Reflection.Assembly.Load(aname);
} catch {
return null;
Expand All @@ -69,7 +76,7 @@ public class Html2PDF: Snapshot.Html2PDFConverter {
}

if (Instances++ == 0) {
WebCore.Initialize(new WebConfig() { LogPath = s.Compiler.SkinPath, LogLevel = LogLevel.Verbose });
WebCore.Initialize(new WebConfig() { LogPath = path, LogLevel = LogLevel.Verbose });
}

if (Session == null) Session = WebCore.CreateWebSession(new WebPreferences() {
Expand All @@ -84,35 +91,29 @@ public class Html2PDF: Snapshot.Html2PDFConverter {
});
if (View == null) View = WebCore.CreateWebView(1280, 1024, Session);
var config = new PrintConfig();
var size = s.GetSize(s.Element);
config.PageSize = new AweRect(0, 0, (int)size.Width, (int)size.Height);
//s.GetSize(s.Element);
config.PageSize = new AweRect(0, 0, (int)(double.Parse(width)+0.5), (int)(double.Parse(height)+0.5));
config.SplitPagesIntoMultipleFiles = false;
config.Dpi = s.Dpi ?? 600;
path = Path.GetDirectoryName(s.LocalFilename);
var tempFile = path + "/doc_1.pdf";
var Dpi = 600.0;
double.TryParse(dpi, out Dpi);
config.Dpi = Dpi;
IDisposable fileLock = null;
SavedSignal.Reset();
View.PrintComplete += (sender2, args2) => {
File.Move(tempFile, s.LocalFilename);
FileLock.Dispose();
FileLock = null;
SavedSignal.Set();
s.Errors.Message("{0} created.", Path.GetFileName(s.LocalFilename));
s.ImageCreated();
View.PrintComplete += (sender, args) => {
File.Move(args.Files [0], localfile);
Environment.Exit(0);
};
View.PrintFailed += (sender2, args2) => {
s.Errors.Error("Failed converting html to pdf.", "61", s.XElement);
FileLock.Dispose();
FileLock = null;
SavedSignal.Set();
View.PrintFailed += (sender, args) => {
Environment.Exit(1);
};
var loaded = false;
View.LoadingFrameComplete += (sender, args) => {
if (args.IsMainFrame) {
var part = !string.IsNullOrEmpty(s.ElementName);
Action onloaded = () => {
lock (Lock) {
if (loaded) return;
var part = !string.IsNullOrWhiteSpace(elementname);
if (part) {
View.ExecuteJavascript(@"
var obj = top.document.getElementById('" + s.ElementName + @"');
var obj = top.document.getElementById('" + elementname + @"');
var selection = window.getSelection();
var range = document.createRange();
range.setStartBefore(obj);
Expand All @@ -123,26 +124,29 @@ public class Html2PDF: Snapshot.Html2PDFConverter {
} else {
config.PrintSelectionOnly = false;
}
FileLock = s.Compiler.FileLock(tempFile);
//FileLock = s.Compiler.FileLock(tempFile);
View.PrintToFile(path, config);
loaded = true;
}
};
View.LoadingFrameComplete += (sender, args) => { if (args.IsMainFrame) onloaded(); };
View.LoadingFrameFailed += (sender, args) => { if (args.IsMainFrame) onloaded(); };

if (html != null) View.LoadHTML(html);
else View.Source = new Uri(source);
while (!loaded) { Thread.Sleep(100); WebCore.Update(); }
View.Source = new Uri(source);
while (View.IsLoading) { Thread.Sleep(100); WebCore.Update(); }
Thread.Sleep(300);
WebCore.Update();
onloaded();
while (View.IsPrinting) { Thread.Sleep(100); WebCore.Update(); }
}

public void AwaitSave() {
while (!SavedSignal.Wait(100)) WebCore.Update();

if (FileLock != null) FileLock.Dispose();
if (View != null) View.Dispose();
if (Session != null) Session.Dispose();
if (--Instances == 0) WebCore.Shutdown();
public static void Main(string[] args) {
try {
SaveAsync(args[0], args[1], args[2], args[3], args[4], args[5]);
} catch (Exception ex) {
Environment.Exit(2);
}
}

}

}
Loading

0 comments on commit 7f0199d

Please sign in to comment.