output to file with no output to browser

1,052 views
Skip to first unread message

Hannes2000

unread,
May 17, 2020, 11:54:30 PM5/17/20
to dompdf


stream("filename", array("Attachment"=>1);
writes PDF output to file and browser.
What I want is: output only to file and the file dialog.
How to do this?

M

unread,
May 18, 2020, 12:13:05 AM5/18/20
to dompdf
G'day,

The code I use is this, allowing you to write to file or stream it to download.

$dompdf->loadHtml($rhtml);
$dompdf->setPaper('A4''portrait'); //landscape / portrait

// Render the HTML as PDF
$dompdf->render();
$fileDate=(new DateTime)->format("Y-m-d");
if($saveInvoiceInstead){
    $pdfOutput = $dompdf->output();
    file_put_contents($fileStorage."FileNamePart$id ($fileDate).pdf"$pdfOutput);
}else{
    // Output the generated PDF to Browser
    $dompdf->stream("Invoice$diid ($fileDate).pdf");
}

Hope that helps. :)

Hannes2000

unread,
May 18, 2020, 12:33:45 AM5/18/20
to dompdf
Thank you for the fast answer. No output to browser works, but PDF-File shows no image. But that is another problem.

M

unread,
May 18, 2020, 12:37:49 AM5/18/20
to dompdf
Oh, I think maybe the PDF not showing anything might be an important part of this puzzle.

Can you get your PDF source (HTML, other PDF etc) to show on the screen before you load it into dompdf?

So I understand, by "the file dialog", do you mean the browser dialog that pops up and asks if you want to save a file?

M

Hannes2000

unread,
May 18, 2020, 12:57:25 AM5/18/20
to dompdf
PDF-File content opened in editor makes no sense, so conversion didn't work.
HTML-Input file is showed correctly by browser, dompdf however does not like the file.

Selecting a traget file by File Dialog is not a problem, because it may be done by 
      header('Content-Description: File Transfer'); ...............

Am Montag, 18. Mai 2020 05:54:30 UTC+2 schrieb Hannes2000:

M

unread,
May 18, 2020, 1:01:17 AM5/18/20
to dompdf
I think I might need to understand your intention for this code before we go further. Seeing the code would probably help too.

When you say "PDF-File content opened in editor makes no sense" - if you mean a code editor or notepad or something, then yes - PDFs are not supposed to be readable there. Even if it's HTML in a PDF.

I'm assuming that because you've written "Selecting a traget file by File Dialog" that the user selects a file and that's included in the PDF? is that correct?

M

Hannes2000

unread,
May 18, 2020, 1:14:19 AM5/18/20
to dompdf
PDF was opened with notepad. Target File selection is outside of pdf.


Am Montag, 18. Mai 2020 05:54:30 UTC+2 schrieb Hannes2000:

M

unread,
May 18, 2020, 1:20:15 AM5/18/20
to dompdf
Yeah, ok. The PDF can really only be opened by a program built to open PDFs, Adobe PDF reader or xpdf reader.

What type of file are you selecting to include in the PDF?

~~
For reference: my experience was only with HTML -> PDF. So I may not be able to help fully.

What I did was:
  1. Make the output of the HTML work in the browser
  2. send it to PDF
  3. Work through all the many sizing issues (read: assumptions) that I'd made, plus odd rendering issues.
  4. Work on fonts, and
  5. first page only headers.

M

Hannes2000

unread,
May 18, 2020, 1:36:10 AM5/18/20
to dompdf
To make things easier, I just sendung code. HTML File is a simple file with just
html-statements without includes of images or ...

function convertDompdf($quelleHtml, $zielPdf)
{
    $document = new Dompdf();
    //$document->loadHTMLFile($quelleHtml); 
    $page = file_get_contents($quelleHtml);
    $document->loadHtml($page);
    $document->setPaper('A4', 'portrait');
    $document->render();
    //$zielPdf = rtrim($zielPdf, '.pdf');
    //$document->stream($zielPdf, array("Attachement"=>1)); //Attachement 1 öffnet Dialog
    $pdfOutput  =  $document -> output ();
    file_put_contents ( $zielPdf ,  $pdfOutput );    
}

Am Montag, 18. Mai 2020 05:54:30 UTC+2 schrieb Hannes2000:

M

unread,
May 18, 2020, 1:44:02 AM5/18/20
to dompdf
Hmmmm...

So what I notice about your code vs. my code is that I'm using/setting options.  You might be well served to check through each of those and what they do.

More of my code:

// reference the Dompdf namespace
use Dompdf\Dompdf;
use Dompdf\Options;

// instantiate and use the dompdf class
$options = new Options();
$options->set('defaultFont''OpenSans');
$options->set('rootDir',"/path/goes/here/dompdf/dompdf");
$options->set('tempDir',"/path/goes/here/tmpdir/"); 
$options->set('isHtml5ParserEnabled'true);
$options->set('isPhpEnabled'true);
$options->set('isRemoteEnabled'true);
$options->set('isJavascriptEnabled',true);
$options->set('isFontSubsettingEnabled',true);
$dompdf = new Dompdf($options);

/* // or could do it this way.
    $dompdf->set_option('isHtml5ParserEnabled', true);
    $dompdf->set_option('isPhpEnabled', true);
    $dompdf->set_option('isRemoteEnabled', true);
    $dompdf->set_option('isJavascriptEnabled',true);
    $dompdf->set_option('isFontSubsettingEnabled',true);
    //$dompdf->set_option('debugCss',true);
    $dompdf->set_option('rootDir',"/path/goes/here/fontcache/");
    $dompdf->set_option('tmpDir',"/path/goes/here/tmpdir/");
*/

$dompdf->loadHtml($rhtml);
$dompdf->setPaper('A4''portrait'); //landscape / portrait

// Render the HTML as PDF
$dompdf->render();
$fileDate=(new DateTime)->format("Y-m-d");
if($saveInvoiceInstead){
    $pdfOutput = $dompdf->output();
    file_put_contents($fileStorage."FileNamePart$id ($fileDate).pdf"$pdfOutput);
}else{
    // Output the generated PDF to Browser
    $dompdf->stream("FileNamePart$id ($fileDate).pdf");
}

M

Hannes2000

unread,
May 18, 2020, 2:08:23 AM5/18/20
to dompdf
I just tried an example html-code from dompdf examples, this example code worked ok.
So it is my html wich causes trouble to dompdf. I will try with the option settings.

Am Montag, 18. Mai 2020 05:54:30 UTC+2 schrieb Hannes2000:

Hannes2000

unread,
May 18, 2020, 2:25:30 AM5/18/20
to dompdf
option settings didn't help. I will try modifying html-code step by step.


Am Montag, 18. Mai 2020 05:54:30 UTC+2 schrieb Hannes2000:

Hannes2000

unread,
May 18, 2020, 9:21:13 AM5/18/20
to dompdf
It works now. I don't know exactly which HTML statement dompdf disliked.
Initialy I first tried with Html2Pdf and I changed to dompdf because of the many HTML limitations of Html2Pdf.
But I still had adaptions to Html2Pdf in the generation of HTML, that may caused the problem.
Thank you very much for your help.

M

unread,
May 18, 2020, 7:01:08 PM5/18/20
to dompdf
Wunderbar! froh, dass ich helfen konnte. :)
Reply all
Reply to author
Forward
0 new messages