I have some problems how to show PDF in Laravel.
What I did is:
web.php
Route::get('/datenschutz', function() {
$pdf = \PDF::loadView('pdf.Datenschutzerklarung');
return $pdf->stream('Datenschutzerklarung.pdf');
} )->name('getDatenschutzerklärung');
And I have that file in that folder, it loads great, but problem is, file is empty!
How I can post as HTML that pdf that I have?
I guess I have to convert that file in HTML, but I can't include third part 'pdf converters'.
Related
I use laravel dompdf to create PDF files. What I'm trying to do is create pdf file, upload it to storage and then download it. there's specific endpoint, when user call it all these actions happen.
// fetch data for pdf
$pdfData = $this->getData();
// creates pdf from html
$pdf = PDF::loadView('pdf.example', [
'items' => $pdfData
])->setPaper('a4', 'landscape');
// upload file to storage
Storage::put("/my-path", $pdf->output());
// download file
return $pdf->download("file.pdf");
In html(from which pdf's been created) I have custom fonts(load them with #font-face). the issue is that after calling output method $pdf->output() the characters in pdf are invalid
BUT, if I don't call $pdf->output() characters are shown properly(with my custom fonts). so the issue happens only when I use custom fonts and then call $pdf->output(). any idea how to fix it?
I already saw this solution but it doesn't help, also there's no load_font.php in this package
I'm facing two problems.
force_download() does not work for pdf extensions.
when I try to download a txt extension file the functions work fine
below code.
public function index()
{
$data = "some text";
$name = "sample.txt";
force_download($name,$data);
}
but when I try to do it with a pdf extension, the file gets downloaded properly but when I click the downloaded pdf file it shows Error: Failed to load PDF document.
public function index()
{
$data = "some text for pdf";
$name = "sample.pdf";
force_download($name,$data);
}
secondly is there a way to ask or show a dialogue box before downloading a file instead of force download.
You cannot create a (not currupt) pdf file with force_download(). You'll need to create the pdf file using a php library like fpdf or similar. The force_download() function only generates a header which forces a download to happen.
If the pdf file exists on the server you can force_download('/path/to/my_pdf.pdf', NULL);
concerning the second part of your question, this is too broad, there are thousands of ways to create a dialog box
pass name of the file you want to download
Controller
function download($file_name)
{
$this->load->helper('download');
force_download('./assets/'.$file_name, NULL);
}
View
<a href="<?php echo SURL.'controller_name/download/'.$file_name;?>">Download
</a>
For second question try onclick()
I have a PHP script which generates images from PDF pages using PHP Imagick. In which i write a piece of data on top of the image using annotateImage function and take it as output. This is failing recently on some PDF images being selected. I mean when a specific PDF is being given as input to this program the annotateImage function fails to write data where on many other PDF files it is performing well...
this is the following code which run on a loop to generate this images...
$imagick->setResolution(200,200);
$imagick->readImage('files/pdffile.pdf[1]');
$imagick->annotateImage($draw_small, 1250, 100, 10, 'Hello header');
$imagick = $imagick->flattenImages();
$file = fopen("/outputfile.jpg", 'w+');
chmod($folder_in_action."/outputfile.jpg", 0755);
fclose($file);
$imagick->writeImage('/outputfile.jpg');
$imagick->clear();
The code works well and get the exact PDF page as image in the folder but the annotateImage does not works on specific PDF files and so the output comes with no 'Hello header' message.
The same code works perfect on most of the PDF's where i get the specific PDF page with 'Hello header' message written on top as image...
So could not able to guess what could be the issue...
PDF file which does not allow annotateImage to write text is here:
http://wikisend.com/download/143652/notworking.pdf
PDF which is working (usually most of the pdf files are working properly...)
http://wikisend.com/download/215044/working.pdf
I use the FPDF library to display online PDF files, with the ouput option « I » (which output the content to browser).
Then, I need to be able to save some of those generated pdfs on my server (like does the ouput option « F »).
In order to do that, I wrote this script:
$pdf_data = file_get_contents('https:/HOST/Folder/file.php');
$path =__DIR__.'/Folder/file.pdf';
file_put_contents( $path, $pdf_data );
It succeeds to create a file with pdf extension to the directory, but :
- I got an error: « Format error : not a pdf or corrupted » when I try to open the "pdf" file,
- The saved file is empty (?).
So, what is wrong?
How can I get the content generated by the php file, and save it as a pdf on my server?
Finaly, I forgot this script and found a new solution that works!
I set up two outputs into my PHP code build with FPDF, one to display the file online and one to save the file on the server.
The content generated by the PHP code is always displayed, and I control the PDF backup with a string on the first output.
So, when I need to backup, I use a Jquery function to execute the PHP code in the background and upload the PDF file (hidden iframe works too).
Below the end of my PHP code:
<?php
...
if (mycondition) {$string='F';}
else {$string ='I';}
...
$pdf->Output(__DIR__.'/../../Folder/file.pdf',$string); // save on the server
$pdf->Output('file.pdf','I'); // display on the browser
?>
Below the jQuery function I use:
<script type="text/javascript" src="./js/jquery.js"></script>
<script type="text/javascript">
var phpfile = <?php echo json_encode($Filename); ?>; // the php file to upload
function uploadpdf() {
$.get(phpfile);
return false; }
</script>
Hope this will help!
I have many PDFs that are generated and uploaded to my server.
The problem is they contain the same page three times (3 pages in total with the same content).
My goal is to edit the PDF with PHP so that it contains only one page.
Is there any library that allows me to simply load a PDF and keep only the first page?
Thank you!
Using FPDI, you can create a function to extract the first page of a PDF file:
function first_page ($path) {
$pdf = new FPDI();
$pdf->AddPage();
$pdf->setSourceFile($path);
$pdf->useTemplate($pdf->importPage(1));
return $pdf;
}
Then output the extracted PDF as you would do with FPDF:
// Extract first page from /path/to/my.pdf
// and output it to browser with filename "MyPDF".
first_page('/path/to/my.pdf')->Output('MyPDF', 'I');
FPDF (http://www.fpdf.org/) or MDPF (http://www.mpdf1.com/mpdf/index.php) are great libraries for work with PDF files. I have experiences only with creating PDF; but I assume that one of those libraries can solve your problem.
Edit: Here is some example with FPDF
https://gist.github.com/maccath/3981205