At first i thought TCPDF's GetStringWidth() was not working right, giving me the wrong width:
[----------------------------------------------------------------------------------------------------------------------]
I have some text like $txt = "hallo".
I've tried to get the strings width via the built in function GetStringWidth()
and then create a cell with the strings width.
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setFontSubsetting(true);
$pdf->AddPage();
$pdf->SetFont('times', '', 20);
$pdf->SetCellPadding(0);
$txt = "hey i'm santa clause";
$width = $pdf->GetStringWidth($txt);
$pdf->Cell($width, $h=0, $txt, $border=1, $ln=0, $align='', $fill=false, $link='', $stretch=0, $ignore_min_height=false, $calign='T', $valign='M');
$pdf->Output('example_001.pdf', 'I');
?>
The created cell however is to short for it's content.
If I try the same with courier it works fine.
I think it's a measure of GetStringWidth not processing the font's width right, cause of Courier having the same width for each character, which works.
How can I get GetStringWidth() working
[----------------------------------------------------------------------------------------------------------------------]
Today I tried opening it with Firefox/Evince instead of chrome
Here's the result:
Firefox is on the left, chrome on the right.
The generated PDF file is v. 1.7
Used version of chrome:
google-chrome-beta (41.0.2272.53-1)
What could have gone wrong here? Is it really chromes fault? Or am I missing somethign here?
Thanks
You must try with replacing:
$pdf->Output("{$filename}.pdf", 'I');
with
$pdf->Output("{$filename}.pdf", 'D');
Related
I would like to modify the format of my Hoverinfo in Plotly Dash:
Im working with plotly and not plotly express for some reasons.
My Code:
fig.add_trace(go.Bar(x = summe2[monat_week], y = summe2['Umsatz'], name='Umsatz', offsetgroup = 1, marker_color = 'blue'), secondary_y = False)
fig.update_traces(hovertemplate = "Umsatz:%{y:20,.2f}'+ 'X%{monat_week}: %{x}", secondary_y=False)
At the beginning when i hovered on on my chart it displayed 100K instead of 100,000
With hovertemplate = "Umsatz<:%{y:.2f} I fixed it and now im getting 100000.00 as intended but I have no clue how to set an group delimiter 3 that im getting the 100000.00 displayed
like 100,000.00. I found an older post from here but its not working from me
Link: Plotly: How to format numeric yaxis and hover text?
I Wrote the code for the formatting as in the previous link but nothing changed and maybe you could tell me if i can change the formatting to EU like 100.000,00 instead of 100,000.00
via Dash fig.update_traces(hovertemplate = 'Umsatz: %{y: 20,.2f}') should work to display it like 100,000.00 but there is no difference to
%{y: .2f} Display: 100000.00
Thank you in advance
Greetings LittleStudent
I have a question about a WordPress URL in Google Chrome 94.0.4606.81:
I was reading a WordPress article recently and noticed that there is an  (OBJ) symbol in the URL. The symbol is also in the webpage title.
Take Ownership and Select Owner
Question:
What is the purpose of the  (OBJ) symbol -- and how is it possible that it has been included in a URL?
It seems like you got this symbol in the title field of the article. You can remove it from there. If you don't see it select everything in the field with ctrl + a and write the title new.
Honestly, I don't know what nature is this copy/paste issue in WP, and the "Object Replacement Character"
To avoid appearing this character it's enough to use Ctrl+Shift+V shortcut while pasting into WP post title field, means: Paste Text Without Formatting.
If you want to be sure in protecting the post slug (means: post URL) you can use the snippet in your functions.php:
/**
* Remove the strange [OBJ] character in the post slug
* See: https://github.com/WordPress/gutenberg/issues/38637
*/
add_filter("wp_unique_post_slug", function($slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug) {
return preg_replace('/(%ef%bf%bc)|(efbfbc)|[^\w-]/', '', $slug);
}, 10, 6);
preg_replace function searches here for string "%ef%bf%bc" or "efbfbc" (UTF-8 - hex encoded OBJ character) OR any character that IS NOT base alphanumeric character or dash character – to delete.
Since you've mentioned it also made into the title: I use this to filter the title on save to remove these special characters.
function sbnc_filter_title($title) {
// Concatenate separate diacritics into one character if we can
if ( function_exists('normalizer_normalize') && strlen( normalizer_normalize( $title ) ) < strlen( $title ) ) {
$title = normalizer_normalize( $title );
}
// Replace no-break-space with regular space
$title = preg_replace( '/\x{00A0}/u', ' ', $title );
// Remove whitespaces from the ends
$title = trim($title);
// Remove any invisible and control characters
$title = preg_replace('/[^\x{0020}-\x{007e}\x{00a1}-\x{FFEF}]/u', '', $title);
return $title;
}
add_filter('title_save_pre', 'sbnc_filter_title');
Please note that you may need to extend set of allowed UTF range in the preg_replace call based on the languages you support. The range in the example should suit most languages actively used in the word, but if you may write article titles that include archaic scripts like Linear-B, gothic etc. you may need to extend the ranges.
If you copy-pasted it from somewhere, like I did, remember to paste as text using Ctrl + Shift + V to avoid this.
Also, it is the case that this [OBJ] only appears in Chromium-based browsers like Chrome, Edge etc, unlike in Firefox which I believe discards it by default.
I'm trying to figure out how to add line spacing without adding spacing above the very first line of textflow.
This code:
$text = 'For more information about the Giant Wing Paper Plane see ' .
'our Web site <underline=true>www.kraxi-systems.com' .
'the Giant Wing in a thunderstorm as soon as possible.';
$optlist = 'fontname=Helvetica fontsize=12 encoding=unicode leading=400%';
$tf = $p->create_textflow($text, $optlist);
$result = $p->fit_textflow($tf, 28.346, 28.346, 400, 700, 'fitmethod=nofit');
$p->delete_textflow($tf);
results in:
All is good.
Next, I'm increasing the leading option to 400% as:
$optlist = 'fontname=Helvetica fontsize=12 encoding=unicode leading=400%';
And that gives me this:
Question:
How do I keep first paragraph line at the original position and only increase line spacing AFTER it?
checkout the "firstlinedist" option. The default is leading, but you might set this to "ascender" or "capheigt" or any other value.
Please see PDFlib 9.2 API reference, chapter 5.2, table 5.12 for more details.
Is there a way to set print page size in millimeters, like you can with borders, in NReco - the html to pdf wrapper for wkhtmltopdf? Found a way to specify one of four page sizes in the docs, which isn't precise enough.
From reading the wkhtmltopdf docs themselves, it seems like it's also limited to predefined page sizes, rather than setting them manually in units of length.
Making this question in case I am, hopefully, wrong. Need to set page to something 15x10cm for example.
Edit: I am having trouble forcing the library to use the html page settings (as an alternative to not setting anything in the html, and setting height/width/margins in NReco, as shown in my partial answer below). This:
#page {
size: 4in 3in;
margin: 0mm 0mm 0mm 0mm;
}
actually works on print, but when I try to force NReco to use it with:
pdfConverter.CustomWkHtmlArgs = "--print-media-type";
it does nothing. Example is from front page of NReco's site too, which makes it funnier.
Looking at this snippet from the wkhtmltopdf documentation:
--page-height <unitreal> Page height
-s, --page-size <Size> Set paper size to: A4, Letter, etc.
(default A4)
--page-width <unitreal> Page width
I would say that using --page-height and --page-width would do the trick. Logic would say that these will set the page height and width in points, but in fact it is mm. If you need to convert, there are 25.4 mm in an inch, and 72 points in an inch.
Note: I will be editing this answer if I get somewhere, but for now, I guess something is better than nothing for whoever might read this.
Posting an answer that others might find helpful but that isn't the full solution to my problem:
HtmlToPdfConverter pdfConverter = new HtmlToPdfConverter();
//The page width and page height values are in mm
pdfConverter.PageWidth = 102;
pdfConverter.PageHeight = 77;
This should NOT be the accepted answer - for some reason it does NOT fully match simple html sizing. For example, if I create an html document with size ratios of 4x3 and then set these props appropriately, the resulting image on the page still does not take up the entire page (ends up smaller).
If I run the following html and set page sizes to 102 mm x 72mm I get the screenshot below, which is way off despite having the ratios right:
<html>
<head>
<style>
.reportBody {
padding: 0px;
border: 0px;
margin: 0px;
}
.reportTable{
padding: 0px;
border: 0px;
margin: 0px;
width: 102mm;
height: 77mm;
}
</style>
</head>
<body class = "reportBody">
<table class = "reportTable">
<tr>
<td style = "background-color:red">
Row 1 Column 1
</td>
<td style = "background-color:blue">
Row 1 Column 2
</td>
</tr>
<tr>
<td style = "background-color:green">
Row 2 Column 1
</td>
<td style = "background-color:yellow">
Row 2 Column 2
</td>
</tr>
</table>
</body>
</html>
Quick answer to your question is yes, the settings are in mm when you use the height and width properties, so what you are doing is correct with regard to the C# code, your sizing issue is the converter employing a 'smart' resizing technique, which is on by default. The ratio being correct but the dimensions reduced is exactly the same issue I had, which was resolved by the --disable-smart-shrinking option being included.
For a fuller picture:
I've just finished step one of getting prescription printing direct from a new Razor app done, the PDFs being generated via the NReco wrapper. I have to print to a fixed prescription sheet (215mm x 176mm) with a small margin around it. This is the code that I've got in the Controller, which returns the pdf as the fileResult variable.
public async Task<ActionResult> OutputScript(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var model = await GetViewModelForScript(id.Value);
if (model == null)
{
return HttpNotFound();
}
// create a string writer to receive the HTML code
StringWriter stringWriter = new StringWriter();
// get the view to render
ViewEngineResult viewResult = ViewEngines.Engines.FindView(ControllerContext, "Script", null);
// create a context to render a view based on a model
ViewContext viewContext = new ViewContext(
ControllerContext,
viewResult.View,
new ViewDataDictionary(model),
new TempDataDictionary(),
stringWriter
);
// render the view to a HTML code
viewResult.View.Render(viewContext, stringWriter);
// return the HTML code
string htmlToConvert = stringWriter.ToString();
// instantiate the HTML to PDF converter
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
htmlToPdfConverter.CustomWkHtmlArgs = " --print-media-type --title \"SMS Script " + model.ScriptID + "\" --dpi 300 --disable-smart-shrinking";
htmlToPdfConverter.PageHeight = 215;
htmlToPdfConverter.PageWidth = 176;
var margins = new PageMargins();
margins.Bottom = 4;
margins.Top = 4;
margins.Left = 5;
margins.Right = 5;
htmlToPdfConverter.Margins = margins;
htmlToPdfConverter.Orientation = PageOrientation.Landscape;
// render the HTML code as PDF in memory
byte[] pdfBuffer = htmlToPdfConverter.GeneratePdf(htmlToConvert);
// send the PDF file to browser
FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
fileResult.FileDownloadName = "Script.pdf";
return fileResult;
}
The way it is now all the#media print directives are obeyed, hiding the labels that show on the screen "preview" when the PDF is generated for printing.
I'm validating the Tabris framework at the moment and stumbled about a few indication errors when I tried the UI Control Code Snippets.
I added the VisualGuideDemo.java to my HelloWorld-Project and added a table to the display with the createTable function. In my browser it is displaying the expected output with 3 columns and 128 rows. When I test the application on an IPad mini or the IPhone Simulator ...
only the first Column is displayed,
it is not possible to scroll through the whole list (i can scroll about 60 items down, then the list "jumps" back)
and the width of the table does not adjust to the fullwidth of the screen.
See attached Screenshots
I also tried to add a TabFolder which uses the full width of the screen. I used the 'createTabFolder' method:
private void createTabFolder() {
final Shell shell = new Shell( display, SWT.NONE );
shell.setLayout( new FillLayout() );
final TabFolder tabFolder = new TabFolder( shell, SWT.NONE );
final TabItem tab0 = new TabItem( tabFolder, SWT.NONE );
tab0.setText( "Tab0" );
final TabItem tab1 = new TabItem( tabFolder, SWT.NONE );
tab1.setText( "Tab1" );
final TabItem tab2 = new TabItem( tabFolder, SWT.NONE );
tab2.setText( "Tab2" );
//shell.setSize( 300, 100 );
shell.open();
}
I uncommented the setSize function, but the TabFolder does not adjust to the full width of the screen (not in browser or on iOS device).
I'm new to SWT, RAP/Tabris so I am wondering if I miss something basic.
EDIT:
I guess several columns in one table are not supported in the mobile clients, but maybe it would be a nice feature to add the missing columns to the end of the list, seperated by listdividers. Is my assumption correct?
I found the solution to the width problem, I just had to call shell.setMaximized(true);