cp79shark / shark.pdfconvert Goto Github PK
View Code? Open in Web Editor NEWSimple .NET Core wrapper for WkHTMLToPDF tool.
License: MIT License
Simple .NET Core wrapper for WkHTMLToPDF tool.
License: MIT License
Hello,
I have a problem with zoom and page size settings i receive error from wkhtmltopdf, this is my config:
Shark.PdfConvert.PdfConversionSettings config = new Shark.PdfConvert.PdfConversionSettings
{
Title = "Converted by Shark.PdfConvert",
LowQuality = false,
Margins = new Shark.PdfConvert.PdfPageMargins() { Bottom = 10, Left = 10, Right = 10, Top = 10 },
Size = Shark.PdfConvert.PdfPageSize.A4,
Zoom = 1.2f,
OutputPath = @"C:\DATA\pdfSharkTest.pdf"
};
this is the error i receive:
WkHTMLToPdf conversion of HTML data failed. Output: \r\nWkHTMLToPdf exited with code 1.
am I using it wrong? :) without Zoom and Size options it generates file normally
Or anything else in the PdfConversionSettings for that matter? Like adding a logo on all generated document.
Since PdfConvert is global static, it seems making sense to have global settings as well?
Thanks.
Since this framework use WkHtmlToPdf and WkHtmlToPdf is under LGPLv3 which is a heavy copy left license, can this FW be licensed under MIT? I know its not directly distributing WkHtmlToPdf as part of the distro package, however to make use of the package a developer needs WkHtmlToPdf .
Hello cp79shark,
Done a good job. It was working good, a few things to be improved If you fix those things your product will be rock, I will tell you something which I have found from my side.
This is for your kind information
Need to implement tests for all the WkHTMLToPDF options. Currently there are just tests to match the samples.
It doesn't seem like this library is getting much attention anymore, but I figure I'll put this here just in case the developer sees it. It appears the library is throwing an IOException when attempting to delete one of the temporary files it creates in C:\WINDOWS\TEMP. Here is the error I got:
The process cannot access the file 'C:\WINDOWS\TEMP\ce20a607-f3c8-4116-bd58-74a731b790b0.html' because it is being used by another process.
STACKTRACE: at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.InternalDelete(String path, Boolean checkHost)
at System.IO.File.Delete(String path)
at Shark.PdfConvert.PdfConvert.Convert(PdfConversionSettings config, Stream pdfOutputStream, Stream contentInputStream, Stream coverInputStream, Stream footerInputStream, Stream headerInputStream, Action`2 outputCallback)
at .ConvertToPdf(String& htmlText) in :line 67
I know for a fact I'm not doing anything in the temp directory in my application, and when I dug down into this library I saw that you're creating and writing the html content into a temporary file in the format of ".html" out at C:\Windows\Temp.
I looked through the code to see where the file is getting used, and I'm unable to tell exactly which file it is that is having the issue when being deleted. I'm using this library in a small print service that my server uses, it seems like the issue is happening early in the morning. My sneaking suspicion is that Windows is locking down this file somehow because of where it lives, but I don't know enough about Windows to make that kind of conclusion. I'll be running a test on my end here to see if I can replicate the issue after not printing something for a while and coming back in in the morning and trying again and see if the issue repeats. If that's the case, I'll likely just fork off the project and modify the code to cause the temporary files to be created in a folder I know the system won't fiddle with and see then if the issue persists. As far as I can tell, the library is properly handling files, and it's not an immediate logic problem.
Unhandled exception. System.ArgumentException: File 'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe' not found. Check if wkhtmltopdf application is installed and set the correct location before calling this method.
at Shark.PdfConvert.PdfConvert.Convert(PdfConversionSettings config, Stream pdfOutputStream, Stream contentInputStream, Stream coverInputStream, Stream footerInputStream, Stream headerInputStream, Action`2 outputCallback)
at .Program.Main(String[] args) in /source/App/Program.cs:line 11
Hi,
Thanks for a great tool! It works great for me, apart of one fairly big issue. When I generate the PDF I point the tool to the URL of another application, but I also need to pass the authorization token together with the request. Currently I don't know how to do it. I have looked into WkHtmlToPdf documentation and found the --custom-header option in the tool, but I'm not sure how to implement it into your code. Any chance you could help me with that?
Thanks
Library has worked well for me so far, is there any plan to support multiple urls for a combination into a single PDF?
I've been using the following method which requires some specific ordering of parameters to get the required outcome for multiple web pages.
var config = new Shark.PdfConvert.PdfConversionSettings
{
Title = "Combined PDF",
ContentUrl = "thirdurlrequired" //Will render as last pages in PDF
}
var customArgs = new List();
customArgs.Add("--javascript-delay 1000");
customArgs.Add("--print-media-type");
customArgs.Add(string.Format("page \"{0}\" ", "firsturlrequired")); //Additional pages must come as last args, first url will render as first doc etc
customArgs.Add(string.Format("page \"{0}\" ", "secondurlrequired"));
config.CustomWkHtmlArgs = string.Join(" ", customArgs);
Is it possible that the keyword replacer for 'frompage', 'topage', 'page', 'webpage', 'section', 'subsection', 'subsubsection' is not working or not implemented correctly for the headerHtml and footerHtml. You know, where you should just type <span class="page"></span>
and the inside the <span>
there should be the page number.
I fixed this by inserting the subst()
JS script and calling it onload
of <body>
in my html-file like this.
...
<script>
function subst() {
var vars={};
var x=document.location.search.substring(1).split('&');
for (var i in x) {var z=x[i].split('=',2);vars[z[0]] = unescape(z[1]);}
var x=['frompage','topage','page','webpage','section','subsection','subsubsection'];
for (var i in x) {
var y = document.getElementsByClassName(x[i]);
for (var j=0; j<y.length; ++j) y[j].textContent = vars[x[i]];
}
}
</script>
</head>
<body onload="subst()">
...
Maybe this helps somebody.
When adding a cover page header and footer are not rendered. This seems to be a problem with wkhtmltopdf it self wkhtmltopdf/wkhtmltopdf#1676 but I got it to work with your code by just putting the header and footer part before cover, so they are global, and now it works.
Running 0.12.4 of wkhtmltopdf.
I'm posting this as an issue since I can't do a pull-request at this time.
private static string BuildOptions(PdfConversionSettings config,
string temporaryContentFilePath,
string temporaryPdfFilePath,
string temporaryCoverFilePath,
string temporaryHeaderFilePath,
string temporaryFooterFilePath)
{
StringBuilder options = new StringBuilder();
// GLOBAL OPTIONS
if (string.IsNullOrWhiteSpace(config.CustomWkHtmlArgs))
{
if (config.Grayscale) options.Append("--grayscale ");
if (config.LowQuality) options.Append("--lowquality ");
if (config.Margins.Bottom != null) options.AppendFormat("--margin-bottom {0} ", config.Margins.Bottom);
if (config.Margins.Top != null) options.AppendFormat("--margin-top {0} ", config.Margins.Top);
if (config.Margins.Left != null) options.AppendFormat("--margin-left {0} ", config.Margins.Left);
if (config.Margins.Right != null) options.AppendFormat("--margin-right {0} ", config.Margins.Right);
if (config.Size != PdfPageSize.Default) options.AppendFormat("--page-size {0}", config.Size.ToString());
if (config.Orientation != PdfPageOrientation.Default) options.AppendFormat("--orientation {0} ", config.Orientation.ToString());
if (string.IsNullOrWhiteSpace(config.Title) == false) options.AppendFormat("--title \"{0}\" ", config.Title.Replace("\"", ""));
}
else
{
options.Append(config.CustomWkHtmlArgs);
options.Append(" ");
}
// FOOTER
if (string.IsNullOrWhiteSpace(temporaryFooterFilePath) == false ||
string.IsNullOrWhiteSpace(config.PageFooterUrl) == false)
{
options.AppendFormat("--footer-html \"{0}\" ",
string.IsNullOrWhiteSpace(config.PageFooterUrl) ? temporaryFooterFilePath : config.PageFooterUrl);
if (string.IsNullOrWhiteSpace(config.CustomWkHtmlFooterArgs) == false)
{
options.Append(config.CustomWkHtmlFooterArgs);
options.Append(" ");
}
}
// HEADER
if (string.IsNullOrWhiteSpace(temporaryHeaderFilePath) == false ||
string.IsNullOrWhiteSpace(config.PageHeaderUrl) == false)
{
options.AppendFormat("--header-html \"{0}\" ",
string.IsNullOrWhiteSpace(config.PageHeaderUrl) ? temporaryHeaderFilePath : config.PageHeaderUrl);
if (string.IsNullOrWhiteSpace(config.CustomWkHtmlHeaderArgs) == false)
{
options.Append(config.CustomWkHtmlHeaderArgs);
options.Append(" ");
}
}
// COVER
if (string.IsNullOrWhiteSpace(temporaryCoverFilePath) == false ||
string.IsNullOrWhiteSpace(config.PageCoverUrl) == false)
{
options.AppendFormat("cover \"{0}\" ",
string.IsNullOrWhiteSpace(config.PageCoverUrl) ? temporaryCoverFilePath : config.PageCoverUrl);
if (string.IsNullOrWhiteSpace(config.CustomWkHtmlCoverArgs) == false)
{
options.Append(config.CustomWkHtmlCoverArgs);
options.Append(" ");
}
}
// TABLE OF CONTENTS
if (config.GenerateToc)
{
options.Append("toc ");
if (string.IsNullOrWhiteSpace(config.CustomWkHtmlTocArgs) == false)
{
options.Append(config.CustomWkHtmlTocArgs);
options.Append(" ");
}
}
// PAGE
options.AppendFormat("page \"{1}\" \"{0}\" ",
temporaryPdfFilePath,
string.IsNullOrWhiteSpace(config.ContentUrl) ? temporaryContentFilePath : config.ContentUrl);
// PAGE OPTIONS
if (string.IsNullOrWhiteSpace(config.CustomWkHtmlPageArgs))
{
if (config.Zoom != null) options.AppendFormat("--zoom {0} ", config.Zoom);
}
else
{
options.Append(config.CustomWkHtmlPageArgs);
}
return options.ToString();
}
I need to create a pdf file with custom page width and height in mm.
I have tried to set in on the configuration but it seems not working. I have checked the source code and it seems that this parameter is not used on the rendering process of the pdf file.
Can you please provide support for it?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.