Rotativa - Wkhtmltopdf: Add footer chinese text? - c#

I am trying to add a footer to the page that shows the page number what looks like this
"Page 1 of 10" in Chinese using Rotativa HTML to PDF library.
This is set by the CustomSwitches attribute.
My code is like this
ViewAsPdf pdfView = new ViewAsPdf("Index", Model)
{
FileName = fileName,
PageOrientation = pageOrientation,
PageSize = Size.A4,
IsJavaScriptDisabled = isJavaScriptDisabled,
CustomSwitches = "--disable-external-links --disable-internal-links --footer-center \"Page [page] of [toPage]\" --footer-font-size 7"
};
This works great.
But if I change the text to Chinese it does not render the characters right
--footer-center \"第 [page] 页, 共 [toPage] 页\"
I understand the this CustomSwitches calls wkHtmltoPDF, but did not find anything there either. The html page that contains Chinese characters renders just fine.

The answer is that the custom switches are a command line argument for the WkhtmltoPDF.
In order to make it work you have to go to Control panel -> Region and language -> Administrative -> Change system locale... -> in this case I selected Chinese (Simplified, PRC)

Related

SelectPDF HtmlToPdf not rendering all pages

I am using the SelectPDF library for my ASP.NET web application to convert a dynamically built HTML to PDF and then print it. The conversion simply looks like this:
HtmlToPdf converter = new HtmlToPdf();
converter.Options.PdfPageSize = PdfPageSize.A5;
converter.Options.PdfPageOrientation = PdfPageOrientation.Landscape;
PdfDocument doc = converter.ConvertHtmlString(FormattedLabelHTML);
doc.Save(Response, false, "Label_" + hfDispatchID.Value + ".pdf");
doc.Close();
The HTML itself is fine, as looking at the FormattedLabelHTML string in debug mode shows it's complete and looks at it should be (it's just HTML tables for each page, with page brakes between them).
But if the number of pages that should be printed is more than 12, then the rendered content stops at 12 pages, leaving the 12th page half-rendered, and the rest completely missing. If the page count is under 12, then they all render properly.
There are no errors thrown, everything executes properly. So does anyone have any tips as to why it can't print more than 12 pages?

Adding a glyphicon to a control through c#

Dim glyph As HtmlControls.HtmlGenericControl
glyph = New HtmlControls.HtmlGenericControl
glyph.InnerHtml = "<span class=""glyphicons glyphicons-restart""></span> Reset"
btnBarButtonBar.SummaryButton.Controls.Add(glyph)
I currently am doing it through VB like this but could anybody convert this for me to C# please? I just need to replace the current summary button which is built through the framework and directs the user to another page and replace it with this button which resets all values to blank. I have the .asp pages working already using the above code, I now need to convert to so it works in aspx/c#
var glyph = new HtmlControls.HtmlGenericControl();
glyph.InnerHtml = "<span class=\"glyphicons glyphicons-restart\"></span> Reset";
btnBarButtonBar.SummaryButton.Controls.Add(glyph);

Rotativa PDF MVC3 Paging

Am using Rotativa to create pdfs in my mvc4 application. Problem is how do i get display page numbers on the pdfs.
This code will give you a page number at the center of the footer.
return new Rotativa.ViewAsPdf()
{
CustomSwitches = "--page-offset 0 --footer-center [page] --footer-font-size 8"
}
Use --page-offset option to set the starting page number.
Set the page offset to 0 and the page number will start at 1 (This is also the default option).
Font size can be controlled by specifying the --footer-font-size option.
You should be able to leverage page footer options on wkhtmltopdf by adding a CustomSwitches property to Rotativa *AsPdf.
Take a look at http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf-0.9.9-doc.html

Adding HTML property in C# when dealing with email

I am facing a situation where I need to process an incoming email which is in plain text format and then display the email as virtual plain text(Font Size =10.5 and Font type=Consolas) to the user.
My code till now
string Text = "<html><body><basefont size=3 font face= consolas>" + mailItem.HTMLBody + "</font></body></html>";
mailItem.HTMLBody = Text;
//Process the email and display
This works good, but during display this shows virtual plain text of font size 10.0 instead of needed 10.5. I tried changing the basefont size to 4,5 etc. this does not change the display in any way.
other options tried:
1.Adding a CSS stylesheet. This will not work good as most email clients don't support it.
2.Adding a div tag to the code like this
string Text = "<html><div style=font-size:10.5px; font-face:consolas;>" + mailItem.HTMLBody + "</font></body></div></html>"
this also does not work.
My main objective is to display a virtual plain text email with Font Size 10.5 and font type Consolas to the user after the processing has been done.
Your second approach seems to be allright - you just have a </font> and a </body> too much + you should change "font-face" to "font-family".
Also throw in double quotes - that will output one quote:
string Text = "<html><div style=""font-size:10.5px; font-family:Consolas;"">" + mailItem.HTMLBody + "</div></html>"

Show more/less without stripping Html tags in JavaScript/jQuery

I want to implement readmore/less feature. i.e I will be having html content and I am going to show first few characters from that content and there will be a read more link in front of it. I am currently using this code :
var txtToHide= input.substring(length);
var textToShow= input.substring(0, length);
var html = textToShow+ '<span class="readmore"> … </span>'
+ ('<span class="readmore">' + txtToHide+ '</span>');
html = html + '<a id="read-more" title="More" href="#">More</a>';
Above input is the input string and length is the length of string to be displayed initially.
There is an issue with this code, suppose if I want to strip 20 characters from this string:
"Hello <a href='#'>test</a> output", the html tags are coming between and it will mess up the page if strip it partially. What I want here is that if html tags are falling between the range it should cover the full tag i.e I need the output here to be "Hello <a href='#'>test</a>" . How can I do this
Instead of doing too much manually, I prefer you to go with following jQuery plugin.
http://plugins.learningjquery.com/expander/index.html#getting-started
It will expand and collapse your text for Read more options, Also provide you much customization.
Hope this will help.
Thanks!
Hussain
This will be quite hard since HTML is not a regular language, so using regular expressions would be cumbersome.
Really want you need to do is parse the HTML into a DOM structure using jQuery $('html text') and then recursively go through the elements getting the length of each elements text using .text() and when you come to an element with causes the cumulative text length thus far to be over the limit, split this elements text at that point and set it the element's text to be the left of the split.
Then keep all elements before this one, and disregard all siblings etc that occur after this element. However I have no idea how to implement that simply.
On the other hand you could just strip the HTML tags away, why bother.
If you have the text in a dom element, you could use the innerText/textContent property of the dom element to get just the text without the tags. substring of this text could be used without worrying about the tags.
var post_dom = document.getElementById('#post-content');
var input = post_dom.innerText || post_dom.textContent;
I think the best solution here would be to use
var textToHide = input;
var textToShow = $("<div/>").html(input).text().substring(0, length);
which will convert your HTML to plain text and extract the first few characters for the summary. Then toggle between them when they click the ellipsis rather than trying to display both at the same time.
maybe you could rethink the problem : instead of modifying your text, you could modify the way it is shown on the page.
Say if you want to show only 3 rows of the content, you could modify the content's container style to a height equal to the height of one content row * 3.
Anyway, this could be easily implemented in jquery ,and if you take advantage of it's animate function, you could easily make your own plugin such as :
(function($){
$.fn.lessDetails = function(desiredHeight){
var desiredHeight = desiredHeight || 20; //or any other "default" value
return this.each(function(i){
$(this).realHeight = $(this).css("height");
$(this).css('height',desiredHeight);//or you could animate it
});
};
$.fn.moreDetails = function(){
return this.each(function(i){
$(this).css('height',$(this).realHeight || "auto");//or you could animate it
});
};
})(jQuery);
and then you could call :
$('.less').lessDetails(); $('.showMore').click(function(){$('#'+$(this).attr('rel')).moreDetails();});'
having your html structured as :
< div class="container"> < div id="1" class="less">content goes here </div> < a href="#" rel="1" class="showMore"> show more</a> </ div >
Haven't tested it ,but it should work, or at least should give you a basic idea of my solution.

Categories

Resources