mpdf only displays in firerox

Asked

Viewed 1,075 times

2

I am using mPDF and it is only generating the reports correctly in Firefox or opening by IE. If I try to open the file with PDF reader it returns the failure error when loading PDF document.

<?php 
 include("../crud/_views/plugins/mpdf60/mpdf.php");

 $html = "
teste
 ";



$mpdf = new mPDF('','', '', '', 55, 18, 19, 15, 0, 0, '');
$css = file_get_contents('../crud/_views/plugins/mpdf60/estilo.css');
$mpdf->WriteHTML($css,1);
$mpdf->WriteHTML($html,2);
$mpdf->Output('');

By the code I noticed that the error is generated on the line $mpdf->WriteHTML($css,1);, if I remove it the PDF is displayed normally but does not stick to the css style.

css style file.

    .titulo{
    font-size: 14px;
    font-family: Times, "Times New Roman";
    font-weight: bold;
}
.sub-titulo{
 font-size: 12px;
 font-family: Times, "Times New Roman";
 font-weight: bold;
}
.numero{
    text-decoration: underline;
}

.direita{
 text-align: right;
}

.center{
 text-align: center;
}

.alinhamento{
 margin-right: : 10%;
}

.direita{
 text-align: right;
}
  • What version of MPF and what $css and $html content?

  • The latest version of the MPDF, the content is in the code, the $css is just the link reference, according to the mpdf https://mpdf.github.io/css-stylesheets/introduction.htmldocumentation

  • Last version is what? The mpdf has in more than one site to download, people think they are picking up on the official site when they are not and think it is the "last version" when it is not. Informs the exact version. And please, I’ve asked, inform the contents of $html.

  • 1

    I have already solved with the answer below, thank you!

  • Rafael solved himself with the answer below, which has no logic with the explanation of the doc that indicates that there is some problem in the version you are using and so it is important to inform this kind of detail. Clearly the answer is only a palliative solution and does not explain why.

  • All right, the version is 6.0 taken from the site http://www.mpdf1.com/mpdf/index.php and using your manual as a reference. I edited the question with the full content of page q generates pdf

  • As I said, 6.0 is not the latest version, and as far as I know the site that officially distributes the MPDF is their own github repository: https://github.com/mpdf/mpdf/releases or via Composer (mpdf version 6.1 and 7 only). I edited my answer and restored it.

  • Rafael, as soon as I entered the mpdf1 site I saw the following messages: Updated 16/03/2016 - This mPDF website is now closed down. In other words, this site is inactive, unlike the github whose mpdf author keeps keeping the updates and fixes. I added it to the body of the reply. Note: When your HTML does not have the <HEAD> tag then it is necessary to use the second parameter with the value 2

  • I’ve updated the answer with all the details, so you can imagine that I’m getting on your bad side or your nagging, but that’s not it. The reason is that you received a good response from Samuel, but I noticed the problem that can be more serious, you are using an outdated version of which can contain Bugs and this would cause you problems, so I insist on the reply and comments I sent you, because sometimes things are more complex and my intention is to help you showing where is the serious problem of using a tool that can contain BUGS.

Show 4 more comments

2 answers

4


From what I understand, you are trying to generate a PDF by putting a code in . css on it, hence the error.

Try referencing the css within your html, and remove the line $mpdf->WriteHTML($css,1);.

To reference the css inside your html, just put the following line:

<link rel="stylesheet" type="text/css" href="[CAMINHO_DO_ARQUIVO_CSS]">

  • It worked that way, I just didn’t understand why it didn’t work according to the manual reference. Thank you!

  • Not at all. And if you can, please mark the answer as correct.

1

Probably the version you’re using of mpdf is not the latest version as you stated, as the most updated version of MPDF has namespace:

<?php

require_once __DIR__ . '/vendor/autoload.php';

$mpdf = new \Mpdf\Mpdf();

Or:

use Mpdf\Mpdf;

require_once __DIR__ . '/vendor/autoload.php';

$mpdf = new Mpdf();

It doesn’t mean that you can not use versions 5 and 6 of the MPDF, I am only commenting on this because you have stated strongly that you were using the latter, and by your code I believe not to be.

You also claimed to be using the version 6.0, but the most updated version within "6" is the 6.1 and not the 6.0, that is there may be bugs in the version you downloaded.

The official installation of MPDF 5.7, 6.1 and 7 (the 7 still in beta) are made via composer and https://github.com/mpdf/mpdf/releases (version 6 or lower), if you have not installed via composer or not down from the official repository then probably downloaded a version unofficial or from third party MPDF site, soon can be a version with Bugs and soon you are risking.

Note: You downloaded from the site http://www.mpdf1.com/mpdf/index.php and the site itself has two notes saying:

  • Updated 16/03/2016
  • This mPDF website is now closed down.

That is, these versions that you downloaded have not been updated for a year and this site will no longer receive updates and this "inactive", ie it will probably no longer update, only via github now or Composer.

If you are using PHP 5.6 or higher you can preferably install the latest version using Composer:

 composer require mpdf/mpdf

To install version 6.1 manually download:

An important detail if the fault still continues, according to the official manual:

The use of WriteHtml was like this:

<?php

$stylesheet = file_get_contents('style.css');

$mpdf->WriteHTML($stylesheet,1);

$mpdf->WriteHTML($html,2);

That is, if you are going to inject the CSS directly then the WriteHTML you may have to use the mode 2 if HTML does not contain the HEAD tag, if it contains what I understood is not necessary.

  • I followed all the steps, updated the version. I even took the manually install version on my localhost. I took the example of mpdf itself for css and gave the same error. In firefox opened, when saved in pdf it does not open the file.

  • @Rafaelmeirim now you’ve changed the problem, first you talk not open in Chrome, now you talk, "when saved", are two totally different things. How are you saving this PDF? Using headers or with the MPDF function itself?

  • The error in Chrome and when saving the file it saves normally, however when opening the pdf it does not read with the standard reader, only in firefox, as if it were a corrupted file.

  • @Rafaelmeirim TÁ, but how are you saving? With headers or with the function of the MPDF itself. Poh face again this, in the other question was the same thing, I ask you something, you only answer half or answer something too vague, please friend focuses on reading what is asked, and tries in just one comment to respond to everything if making others have to keep asking 2 or 3 times the same thing. So please explain how you are downloading. Thank you

  • I tested by directly saving the file or displaying it. The problem is not error of my functions itself, as I tested with the source file of the mpdf itself without changing anything and the same error. For example using this default require_once function DIR . '/../vendor/autoload.php';&#xA;&#xA;$mpdf = new mPDF();&#xA;&#xA;$mpdf->SetDisplayMode('fullpage');&#xA;&#xA;// LOAD a stylesheet&#xA;$stylesheet = file_get_contents('mpdfstyleA4.css');&#xA;$mpdf->WriteHTML($stylesheet,1); // The Parameter 1 Tells that this is css/style only and no body/html/text $mpdf->Writehtml($html); $mpdf->Output(); Exit;

  • @Rafaelmeirim I do not know what is "save directly", you pressed Ctrl+S or clicked on a button in the browser called save? Can you explain this properly? Understand something, a word can be very ambiguous, so it should be explained in a technical way what EXACTLY you did, like "I went and X and then opened a window and I clicked on Y and the download occurred", this is a technical and useful explanation, if you say something like "I saved directly" I can’t understand.

Show 1 more comment

Browser other questions tagged

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