HTML to PDF with mpdf table is not centered - html

I have used mpdf to output some html to a pdf. I am not sure why mpdf is not inserting a top or right margin on the PDF yet the left side seems to have one:
I want to just have the content in the horiztonal center as it is cutting off text on the right. I also want to have a top margin as top content is being cut off.
Here is the settings I have set right now:
$mpdf = new mPDF('win-1252', 'Letter', '', '', 10, '', '', '', '', '');
$mpdf->useOnlyCoreFonts = true;
$mpdf->SetDisplayMode('fullpage');

I switched my declaration to the following:
$mpdf = new mPDF('win-1252','Letter', 12, '', 10, 10, 10, 10);
This correctly set the margins.

Related

Dash Unable to Center Table Horizontally

My code below reads in a small CSV file, defines the styles, and displays the output.
The H1 tag is centred, as expected.
But for some reason, my table is not centered horizontally.
What am I doing wrong?
df = pd.read_csv('myfile.csv')
app = Dash(__name__)
hdg_style = {'color': '#055d9c', 'text-align': 'center'}
tblh_style = {'backgroundColor': 'Linen', 'fontWeight': 'semi-bold'}
tbl_style = {'textAlign': 'center', 'maxWidth': '1000px', 'border': 'thin #055d9c solid'}
app.layout = html.Div([
html.H1(children="My Project", style=hdg_style),
dash_table.DataTable(
data = df.to_dict('records'),
columns = [{'id': c, 'name': c} for c in df.columns],
style_cell = {'textAlign': 'center'},
style_header = tblh_style,
style_table = tbl_style)])
if __name__ == '__main__':
app.run_server(debug=True)
I tried the CSS below to center the table horizontally, but it did not work.
style={'margin-left': 'auto', 'margin-right': 'auto'}
If you include the margin attributes in your tbl_style definition, it'll center the table - it did for me in my test case:
tbl_style = {'textAlign': 'center', 'maxWidth': '1000px', 'border': 'thin #055d9c solid', 'marginLeft': 'auto', 'marginRight': 'auto'}
I suspect your attempts with CSS modifications get caught up in the vagaries of Dash configuration of serving static css files.
app.layout = dbc.Container(). In that, you put multiple dbc.Row. Then, dbc.Col in them. In these columns you put objects (your datatable). You can then align anything you wish moving objects by defining size and offset. Check official dash bootstrap components doc for the detail

PDFlib - change origin of text to top-left from default of bottom-left

In pdflib:
$font = $p->load_font("Helvetica", "unicode", "");
$p->setfont($font, 12);
$p->set_text_pos(100, 100);
$p->show('My text');
First arguments of set_text_pos($x, $y) tells library where to position bottom-left corner of text element.
Question. How can I change the origin of text to be top-left corner, so that "My text" string appears below 100x100 coordinate?
I would recommend to use the more comfortable fit_textline() call. In this case, you can replace your four lines with a single line:
$p->fit_textline('My text', 100, 100, 'fontname=Helvetica encoding=unicode fontsize=12 position {top left} ');

TCPDF html render not indented

I'm trying to display data with TCPDF from my DB which were specifically indented.
I use a writeHTMLCell to do so.
e.g. :
"This should be indented as so :
- First bullet
- Second one
Then the end of the example."
But of course, it render the text on a single line.
Does anyone know how i can render the text properly ?
Thank you very much !
With HTML preformatted text requires that you use <pre>. Here is an example:
This should be indented as so:
- First bullet
- Second one
Then the end of the example.
<pre>
This should be indented as so:
- First bullet
- Second one
Then the end of the example.
</pre>
Using <pre> tags with the preformatted text block should achieve the result you are looking for. Preformatted text defaults to a monospaced font and can be changed with $pdf->SetDefaultMonospacedFont(). Here is an example with and without <pre>:
<?php
require_once('tcpdf_include.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->AddPage();
$text_sample = 'This should be indented as so:
- First bullet
- Second one
Then the end of the example.';
$y = $pdf->getY();
$pdf->SetDefaultMonospacedFont('helvetica');
$pdf->SetFont('helvetica', '', 14, '', true);
$pdf->SetFillColor(255, 255, 255);
$pdf->SetTextColor(255, 92, 92);
$pdf->writeHTMLCell(190, '', '', $y, $text_sample, 1, 0, 1, true, 'J', true);
$pdf->SetTextColor(53,183,41);
$pdf->writeHTMLCell(190, '', '', $y+20, '<pre>'.$text_sample.'</pre>', 1, 1, 1, true, 'J', true);
$pdf->Output('example.pdf', 'I');
The PDF will contain the following cells:

mpdf table width to fit A4 paper size

I have been trying every possible solution to make table fill the width of the A4 paper. I am using mpdf to convert HTML to pdf. But table is never 100% width.
I tried giving width in 100%, 1000px, 180mm but none of them worked. Please Help.
I tried :
<table width="1000px"> //style="width:100%;" or width:180mm or width:100px
</table>
Nothing worked.
It may help to you, customize page from mpdf configuration:
Fullpage example:
include 'MPDF/mpdf.php';
$pdf = new mPDF();
$pdf->ignore_invalid_utf8 = true;
$header ="Header html file or code";
$footer ="Footer html file or code";
$content ="Body html or code";
$pdf->SetDisplayMode('fullpage');
$pdf->SetHTMLHeader($header);
$pdf->SetHTMLFooter($footer);
$pdf->WriteHTML($content); // write the HTML into the PDF
$pdf->Output("pdf/test.pdt", 'F'); // save to file because we can
or you can use custom margin:
include 'MPDF/mpdf.php';
$pdf = new mPDF();
$pdf->ignore_invalid_utf8 = true;
$header ="Header html file or code";
$footer ="Footer html file or code";
$content ="Body html or code";
$pdf->SetHTMLHeader($header);
$pdf->SetHTMLFooter($footer);
$pdf->AddPage('', // L - landscape, P - portrait
'', '', '', '',
15, // margin_left
15, // margin right
30, // margin top
30, // margin bottom
10, // margin header
10 // margin footer
);
$pdf->WriteHTML($content); // write the HTML into the PDF
$pdf->Output("pdf/test.pdt", 'F'); // save to file because we can

chrome not displaying pdf right?

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');