I have a piece of code which polls a CRM system and finds a specific document via a URL..... from the document which is found, this is then added to a PHP variable - however if I then print the PHP variable it's just garbage. How can I take the PHP variable and out the contents to PDF (it is a PDF already stored in the variable).... so it can be either downloaded or opened to screen?
Here is the code being used:
**$url = "accounting/invoices/55120.pdf?template=30&api_key='My-API-Key'";
$pdf = $thisbooks->get($url, array(), array('decode_json' => false));
// Ensure the response looks like a PDF
if (!preg_match('/^\%PDF\-1\.4\s/', $pdf)) {
$thisbooks->log('ERROR: Unexpected response: is it a PDF?', $pdf, 'error');
}
$thisbooks->log('Fetched PDF', $pdf);
print $pdf;**
Now when I print the $pdf variable I can see the script code, but it doesn't do anything else.....
Thanks in advance
Damian
did you try to use file_get_contents to read your variable?
or use this header: header('Content-type: application/pdf');
If you send your code maybe we can help more.
Related
IM trying to save a PDF file, generated from HTML, into the user's local using MPDF. Here is the part of the code responsable for this:
$mpdf = new \Mpdf\Mpdf();
$html = $this->load->view('gestao/relatorios/relatorio_cargo_cidades', $data, true);
$mpdf->WriteHTML($html);
$mpdf->SetFooter(relatorio_footer());
$mpdf->Output('relatorio_cargos_cidades_seletivo_' . $seletivo_id .'_'.date("Ymd_his").'.pdf', 'D');
At first I was trying to show the PDF using the "I" param from the output function, than the user could just see the PDF and choose to download it or not. But when I tried to submit a file into another website, it says that the file is not a PDF. Than I used my linux to see if the file was actually a PDF, here's what I got:
As you can see, the file is being saved as "data" for some reason. I've already tried to use the 'F' param, also from the output function, and than it worked, I saved as PDF. But the F param only save the file inside the code folder, so it was not very usefull for me.
Can anyone tell me how could I save the file as an actually PDF using MPDF?
EDIT
I think that the problem is being caused by CODEIGNITER, not by the MPDF. When I'm setting the last param from the load->view as TRUE, the returned HTML is in a data (string) form, and MPDF is not converting this properly.
Adding ob_clean() might solve your problem:
ob_clean();
$mpdf->Output('relatorio_cargos_cidades_seletivo_' . $seletivo_id .'_'.date("Ymd_his").'.pdf', 'D');
You can simply use the D parameter for downloading:
$mpdf -> Output ('FILENAME.pdf', 'D');
or generate your own header and then output the content as string:
header ("Content-type: application/pdf");
header ("Content-Disposition: inline; filename=FILENAME.pdf");
$mpdf -> Output ('', 'S');
I tried to use MPDF library for generating PDF.
try {
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('Hello World');
// Other code
$mpdf->Output("1.pdf", 'D');
} catch (\Mpdf\MpdfException $e) { // Note: safer fully qualified exception name used for catch
// Process the exception, log, print etc.
echo $e->getMessage();
}
And I get this error message.
Data has already been sent to output, unable to output PDF file
I used ob_end_clean() but not working.
I used all answers in this question but nothing works for me.
TCPDF & mPDF error: Some data has already been output to browser, can't send PDF file
I have got the same error.
Data has already been sent to output, unable to output PDF file
This means before creating a PDF with mPDF some data is stored in the buffer which is sent to the browser. Therefore it is unable to create PDF.
To rectify this, add this below php built-in function at the first line of your page were you are preparing data for pdf.
ob_start();
And add this below php built-in function before mPDF code (before where you are calling mpdf)
ob_end_flush();
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML($html);
$mpdf->Output();
This will clear the buffer output before processing mPDF.
Make sure if you use any functions to keep them on the same page, don't include a functions page where you have kept your all functions.
Read more about PHP Buffering
I just had this error and the correct solution for it is the following.
if when you type <?php in your script as the very first word, make sure there are no space characters or any characters before <?php whatsoever. This also happens with anything else giving the error Data has already been sent to output. Space characters like space and TAB don't appear to you and it will definitely deceive you.
This won't appear easily to anyone.
For me, it helped to insert <?php instead of <?.
I would like to pull data from a mysql db.
This data is then inserted into a html file which is then converted to a pdf using dompdf.
The template is perfect and display's well when I run call dompdf.
However as soon as I try and insert php code, the template still shows perfectly, how ever the php code is displays nothing. If I open the page its shows, so I know it works.
In the options file I have done this :
private $isPhpEnabled = true;
my php file to call the template (LeaseBase.php):
<?php
$options = new Options(); $options->set('isPhpEnabled','true');
$leasefile = file_get_contents("Leases/LeaseBase.php");
$dompdf = new Dompdf($options); $dompdf->loadHtml($leasefile);
$dompdf->stream(); $output = $dompdf->output(); file_put_contents('Leases/NewLeases.pdf', $output);
?>
I also can't seem to pick up anything in the log files.
Any assistance is appreciated
However as soon as I try and insert php code, the template still shows
perfectly, how ever the php code is displays nothing.
Answer: It shows nothing because when a php page is executed, it outputs html (and not the php code). If you don't have an echo or print or any code that generates html code from the php script, the page will in fact be blank.
It's important to remember that php is serverside code WHICH CAN generate html code as long as you instruct it accordingly.
With versions of Dompdf prior to 0.6.1 you could load a PHP document and the PHP would be processed prior to rendering. Starting with version 0.6.1 Dompdf no longer parses PHP at run time. This means that if you have a PHP-based document you have to pre-render it to HTML, which does not happen when using file_get_contents().
You have two options:
First: Use output buffering to capture the rendered PHP.
ob_start();
require "Leases/LeaseBase.php";
$leasefile = ob_get_contents();
ob_end_clean();
Second: Fetch the PHP file via your web server:
$leasefile = file_get_contents('http://example.com/Leases/Leasebase.php');
...though, actually, if I were loading the file into a variable and feeding it to dompdf without doing any further manipulation I would use dompdf to fetch the file instead. In this way you are less likely to have to deal with external resource (images, stylesheets) reference problems:
$dompdf->load_html_file('http://example.com/Leases/Leasebase.php');
I am generating a PDF document with a FPDF library.
I am trying to display a generated document in browser (Firefox, Chrome, Opera) but all i get are strange characters starting with:
%PDF-1.3 3 0 obj <>...
This is my code. I am trying to add two images to the document.
$pdf = new FPDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->Image('./image.png',10,10,0,0,'png');
$pdf->Image('./image.png',10,120,0,0,'png');
$pdf->Output('file.pdf','I');
If I use the output with the 'D' option:
$pdf->Output('file.pdf','D');
and save the file on the disc, the pdf document is fine.
You probably need to add the correct content type header. When the web server serves up a file, it tries to give the correct content type header it guesses from the file, but here, as you're generating it with PHP, the default header will be sent, which is usually text/html. Try adding this to the start of your code:
header('Content-type: application/pdf');
And it should send with the correct content type header.
I run into the same problem and I add ob_get_clean(); before $pdf->OutPut() and I got the correct result.
It was because the header was not correct, but even I set the header before outputting the pdf, it still not correct.
change D to I
$pdf->Output('file.pdf','I');
send the file inline to the browser. The plug-in is used if available. The name given by name is used when one selects the "Save as" option on the link generating the PDF.
from there documentation
fpdf documentaion
Add exit as follows :
$pdf->Output();
exit;
Reference
Try:
ob_start (); //used before output.
$pdf->Output();
?>
I don't know if i should put it as a comment or answer, i had same issue on my localhost, but once i placed my code on live server the issue resolved automatically. Try cleaning all cookies on your local system and it may help too.
I had applied all the above options with different sequences and this is how finally it works. Thanks
$str = iconv('UTF-8', 'windows-1252', $custom_data);
$final = html_entity_decode($str ,ENT_QUOTES, 'windows-1252');
$pdf = new FPDF();
header('Content-type: application/pdf');
$pdf->AddPage();
$pdf->SetFont('Courier','',16);
$pdf->MultiCell(190, '10', $final, '', 'L');
ob_get_clean();
$pdf->Output($afterDash.".pdf", "I");
exit;
In my site, I have implemented the following functionality: If the user clicks on a button it triggers a PHP function which generates an XML file (that PHP function is called by AJAX). Everything is working well, but here's one thing I want to change: I don't want an .XML file to be created on the server machine; instead, I want the user to be prompted to save the .XML file locally. How do I do that? My PHP script currently looks like this:
$xml = new DOMDocument("1.0", "UTF-8");
$rootElement = $xml->appendChild($xml->createElement("SomeNodeName"));
...
// the following doesn't really work - xml doesn't get formatted :)
$xml->formatOutput = true;
// this creates the actual file and places it on server. that's not what i need
$xml->save("MyXMLfile.xml");
Thanks for all the help.
Every web content has a header, so if you specify the header for an xml file (through the use of the header() function with the appropriate code it'll work.
This would mean doing something like this:
<?php
header('Content-type: text/xml');
// set the filename
header('Content-Disposition: attachment; filename="pwet.xml"');
// echo the content here
echo $xml; // simply like this, maybe?