Page Header overlaping content when using Rotativa to generate PDF from HTML - asp.net

I am try to convert an HTML file to PDF using "Rotativa" lib, but i am facing some problems when using custom headers, here is my code:
string customSwitches = string.Format("--header-html \"{0}\" " +
"--header-spacing \"0\" " +
"--footer-html \"{1}\" " +
"--footer-spacing \"10\" " +
"--footer-font-size \"10\" " +
"--header-font-size \"10\" ", header, footer);
var invoice = new ViewAsPdf("Index", _invoiceRepository.GetInvoice(1))
{
FileName = "MyPDF.pdf",
CustomSwitches = customSwitches,
PageMargins = margin,
PageSize = size,
PageOrientation = orientation
};
return invoice;
The headers and Footers are being successfully injected on the top and bottom of each page like expected but the problem is that the header is overlapping that rest of the content.
how can i reserve space for the header?

I faced similar trouble when setting --header-spacing \"0\" once. So the solution that worked for me was to set higher spacing between header and content.
Please try setting, for example, spacing to 7 (measure is given in mm, as you can review in WkHtmlToPdf documentation). This way, you can check current PDF with one you have generated before, and verify if change solves the problem. Once there, you should be ablo to adjust the measure to the value that is suitable for you.
So, your code should look like:
string customSwitches = string.Format("--header-html \"{0}\" " +
"--header-spacing \"7\" " +
"--footer-html \"{1}\" " +
"--footer-spacing \"10\" " +
"--footer-font-size \"10\" " +
"--header-font-size \"10\" ", header, footer);
I tried your code in one of my projects and seems to work fine.
Hope it would be of help. If trouble continues, it should be useful to inlcude some samples os your HTML header and PDF body content, to chek the specific case.

Related

Can you ignore a value in a DLL?

In Grrr.Premium.Google.GoogleGeoCode, the baseUrl is set to http://maps.googleapis.com/maps/api/geocode/, which is failing because it needs to be https.
Is it possible to modify the code below to ignore the value in the DLL and define it here?
Grrr.Premium.Google.GoogleGeoCode encode = new Grrr.Premium.Google.GoogleGeoCode();
encode.Query = "json?key=#######&address=" + Server.UrlEncode(txtAddress.Text.Trim() + " " + txtCity.Text.Trim() + " " + drpState.SelectedValue + " " + txtZip.Text);
encode.GeoCode();
I feel like this must be possible, and easy, but I can't figure it out. :(
Thanks in advance for any help.

PadLeft with spaces

Have a relatively simple request.
I wish to pad left a string with spaces in an HTML page using VB on asp.net
For me the most obvious way to do it is
Response.Write(qty.PadLeft(5, " ") + " x " + part_number)
but as HTML does not render multiple spaces, this does not work
my workaround is
Response.Write(qty.PadLeft(5, "0") + " x " + part_number)
which pads the number with zero's but looks fairly unappealing on the website.
Any suggestions?
Thanks
Update:
Based on replies so far I have tried
"100".PadLeft(5, " ")
but this outputs &&100
OK, I solved it by doing this
rep = string.concat(Enumerable.Repeat(" ", 5-qty.Length))
Response.Write(rep + qty+ " x " + part_number)
Not the best but works.
There's & nbsp; or setting or the css (with white-space) to see spaces. If you want to align information, you might want to also look at having a fixed font.

Replacing a new line character with streamwriter remove everything after it. (ASP.NET, Json, C#)

I'm having an unexpected problem which I'm hoping one of you can help me with.
I have an ASP.NET Web API with a number of end points, one of which takes user input, received as JSON, and converts it into an order object which is written to a file in .CSV format. The following is a small snippet of the full code as an example.
using (StreamWriter writer = new StreamWriter(file))
{
writer.Write(escape + order.Notes + escape + delim);
writer.Write(escape + order.Reference1 + escape + delim);
writer.Write(escape + order.Reference2 + escape + delim);
writer.WriteLine();
writer.Flush();
}
The problem I am having is that some users are inclined to add line breaks in
certain fields, and this is causing havoc with my order file. In order to remove
these new line characters, I have tried both of the following methods.
writer.Write(escape + product.Notes.Replace("\n", " ") + escape + delim);
writer.Write(escape + product.Notes.Replace(System.Environment.NewLine, " ") + escape + delim);
However, it seems that, rather than just remove the new line character and carry on writing the rest of the fields, when a new line is encountered, nothing else gets written.
Either everything else gets replace with the " " or nothing else is being written at all, but I'm not sure which.
If I remove the .Replace() the whole file is written again but with extra line breaks.
I hope somebody has experienced this one and knows the answer!

Webmatrix - formatting MSSQL database query

I am trying to format the output of a query in a WebMatrix 2 CSHTML file. This is the code I am using:
#foreach(var row in db.Query(selectQueryString))
{
#row.Firstname; + " " + #row.lastname; + " " + #row.Entry;
}
I am getting this error:
"CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement"
The first issue is that the semicolons could be confusing to Razor, and they are only confusing matters. So change the line in the brackets to
<text>#row.Firstname #row.lastname #row.Entry</text>
And see if that works. The < text > tags tells Razor to output this directly as HTML and not use it as code. You don't need the + " " because once you're putting out HTML, the spaces come automatically.

create txt file and display it in internet browser inside vfp form

I'm facing difficulty when creating some kind of a report in vfp. I created a text file, used the ?/?? command to write to the file, then opened it using web browser (OLE Object in vfp form).
Set Printer To 'C:\temp\test.txt'
set printer on
set console off
?? 'test'
and then i called my prg :
PARAMETERS pUrl
lcURL = pUrl
PUBLIC oForm
oForm = CREATEOBJECT('IEForm')
oForm.SHOW()
oForm.oIE.Navigate2(m.lcURL)
READ events
DEFINE CLASS IEForm AS FORM
CAPTION = 'Report Preview'
HEIGHT = 512
WIDTH = 792
AUTOCENTER = .t.
ADD OBJECT oIE AS OLECONTROL WITH ;
HEIGHT=512,WIDTH=792,OLECLASS="Shell.Explorer",ANCHOR=195
PROCEDURE oIE.REFRESH
NODEFAULT
ENDPROC
ENDDEFINE
and used the C:\temp\test.txt as the url.
This works fine. But the problem occurs when I use fcreate to create the text file( not using the already existing text file)
lcfile ='C:\temp\'+SYS(2015)+'.txt'
lchandle=FCREATE(lcfile)
Set Printer To lcfile
sometimes the problem occurs when using ? ( resulting in blank doc), and sometimes the browser can't navigate to the file.
The second problem is : the browser can't navigate to the text file if i set the windowtype property to modal in the IEform. I need to set it modal because I have to delay the rest of the execution until the preview form is closed.
I hope I described the situation clear enough.
Thank You for helping Me :)
I would avoid using SET PRINTER TO, and just use the fcreate(), fwrite() and fclose(), especially if writing a large amount of text. If only writing a small amount of data, I would just build one long string by just appending each component to the prior and add cr/lf instead of "?" for new lines, then write the entire result to a file ONCE via StrToFile(). This way, you don't have to worry if things are actually closed (if you dont clear the set printer off, set printer to, or fclose(). Being an incomplete file, the browser won't navigate since the file handle may be "locked" to read anything.
Option 1 -- building a string
lcCRLF = chr(13)+chr(10)
lcMyOutput = "Some Text to Start with" + lcCRLF ;
+ "Today is: " + dtoc( date() ) + lcCRLF
use SomeTable
scan
lcMyOutput = lcMyOutput + "Some Field: " + SomeCharField;
+ "A numeric field: " + transform( SomeNumberField, "999,999.99" ) + lcCRLF
endscan
lcMyOutput = lcMyOutput + "All Done"
lcOutputFile = "C:\Temp\" + sys(2015) + ".txt"
StrToFile( lcMyOutput, lcOutputFile )
Option 2 -- using fcreate(), fwrite(), fclose()
lnHandle = 0
do while .t.
lcOutputFile = "C:\Temp\" + sys(2015) + ".txt"
lnHandle = fcreate( lcOutputFile )
*/ Only get out if we have a proper handle and thus
*/ we are exclusive to it for duration of our process
if( lnHandle > 0 )
exit
endif
enddo
lcCRLF = chr(13)+chr(10)
fwrite( lnHandle, "Some Text to Start with" + lcCRLF ;
+ "Today is: " + dtoc( date() ) + lcCRLF )
use SomeTable
scan
fwrite( lnHandle, "Some Field: " + SomeCharField;
+ "A numeric field: " + transform( SomeNumberField, "999,999.99" ) + lcCRLF )
endscan
fwrite( lnHandle, "All Done" )
fclose( lnHandle )
For running your "batch" file attempt with the RUN command, build the WHAT you want into a string, then run that... ex:
lcCommandToRun = '"' + sys(5) + sys(2003) + '\Compress.tzs"'
run ( lcCommandToRun )
I don't know if it was failing due to a space being in the path of your sys(2003) result turning the RUN into a multi-parameter interpretation, it would fail. By wrapping the entire text within double-quotes, it will prevent such "path" issue.
ADDITIONAL FEEDBACK
Trying to simulate a preview of a DOT-matrix printer using "?" to a text file is not something I would try. I would just do a standard report and draw it with all the data you are doing manually with the "?". If the user want's to preview the report, or send to a laser printer, let the normal report version handled through Windows for generation, font, and such do it for you. Leave the ancient code going to dot-matrix with whatever specific font rendering code as you have. I would just keep the actual report version CLOSE TO your "?" version.

Resources