insert Style into PDF

Asked

Viewed 43 times

0

I have the following content HTML being generated in a loop PHP

$html .= "  
<ul class='border'>
   <li style='text-align: left; border-bottom: .1px rgb(0,0,0) solid;'><label class='labelPequeno'>Gcéu</label> : " . $celula[ "nomeCelula" ] . "</li>
   <li style='text-align: left; border-bottom: .1px rgb(0,0,0) solid;' class='backCinza'><label class='labelPequeno'>Supervisor de Setor</label> : " . $nomeLideres[ "nomeLiderSetor" ] . "</li>
   <li style='text-align: left; border-bottom: .1px rgb(0,0,0) solid;'><label class='labelPequeno'>Líder</label> : " . $celula[ "nomeLider" ] . "</li>
   <li style='text-align: left;'><label class='labelPequeno'>Auxiliar</label> : " . $celula[ "nomeAuxiliar" ] . "</li>
</ul>
<ul class='border'>
   <li style='text-align: left; border-bottom: .1px rgb(0,0,0) solid;'><label class='labelPequeno'>Dias que acontece</label> : " . $celula[ "diaReunioes" ] . "</li>
   <li style='text-align: left;'><label class='labelPequeno'>Membros Ativos</label> : " . $membros . "</li>
</ul>";

This happens in the page x php. for example.

On that same page x php. there is a form that sending, via POST, a field Hidden with the value being the variable above created with your content for a page who will generate an archive pdf.

I’m generating PDF’s with the code below using the library FPDF

require_once "_classes/_util/_PDF/PDF.php";

$html = $_POST["htmlPDF"];

$pdf = new PDF;
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->WriteHTML( utf8_decode ( $html ) );
$pdf->Output("relatorio.pdf","D");

It turns out that in this file I can’t insert calls to style files.

The problem:

$_POST["htmlPDF"] 

Brings a HTML .

In that case, the pdf is leaving without style.

Does there exist a way when being generated to variable that contain the html, enjoy and insert via code (dynamically) the styles straightforward in tags?

1 answer

0

Unless that API of PDF remove the tag <link> you can quietly use a style.css within the body

See here that the tag link is a flow content and is permitted within the body https://www.w3.org/TR/html5/dom.html#flow-content

If an element of link has an attribute rel that contains only keywords that are body-ok, then the element is said to be allowed in the body. This means that the element can be used where the sentence content is expected.

inserir a descrição da imagem aqui Source: https://www.w3.org/TR/html5/links.html#body-ok

Note that even testing in the official validator of W3C is not presented any problem. https://validator.w3.org/#validate_by_input

inserir a descrição da imagem aqui

Code used in the test:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>

    <link rel="stylesheet" type="text/css" media="screen" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
    
    <h1><i class="fa fa-car"></i>Lorem, ipsum.</h1>

</body>
</html>

  • Well, come on. I have a foreach that generates an html structure. Okay? I have a form with an Hidden input with HTML content without the proper Styles embedded in the tags. Okay? This form takes the Valu from Input Hidden to a page that has the code that is in the question. I added the css files before the php block and it went bad. So I wanted to know if there is some kind of $pdf->Setcss and send the linl of the file..

  • @Carlosrocha will not know how to tell you, what I can say, is that even if it is not a very indicated practice you can include the tag link with your file . css right inside the body tag, does not need to be exclusively inside the head tag. (although inside the head tag is advisable)

  • in the file that contains the call to the library can aenas the HTML output. You cannot put the style

  • @Carlosrocha you will not put style you’ll put <link rel="stylesheet" href="../seu.css" /> somewhere there.... I don’t know if it will work, it’s just a hint, and it’s allowed by W3C, already by the API you’re using I don’t know if it works...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.