Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have written a REST application in laravel. It accepts a Json payload and creates a formatted pdf using this data.
Is it possible to write a test that checks the pdf has been generated correctly?
Edit:
Ideally I'd like to know the pdf is not corrupted, ie. Will open in a pdf reader.
Also it would be good to somehow check the content of the pdf. For example... does it contain the customers name?
Thanks.
Yes this is possible, given that the same JSON input always generates the same PDF.
You wouldn't really check the PDF file. PDF is a complex format, based on PostScript and some dark magic.
What you can do is generate a “sample” PDF once, then write a unit test that uses the same input data to generate a PDF file, then compare this to your sample.
This would look something like (just some example code):
$myPdf = $pdfGenerator->generatePdf();
$samplePdf = file_get_contents('/some/example/file.pdf');
// with PHPunit
$this->assertEquals(0, strcmp($myPdf, $samplePdf));
That's a bit dirty, but it does the job … if something in your PDF or JSON implementation changes, the unit test will make you aware of it.
It is important, however, that your PDF generator does not insert any “dynamic” data, such as date stamps. In that case, the PDF files could obviously never be identical.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
i am new developer in PHP and need learn if there is a way to copy content from an excel sheet and paste it into a php site, simulating an excel spreadsheet in PHP and be able to edit the pasted information.
Finally enter this information in a SQL database.
I know that you can upload a file and recover the data from there, but it is not an option in this project
Any advice or help is appreciated.
Thank you
Theres a lot of aspects to what you want to achieve but a few points to get you started. You can use the JExcel plugin in order to simulate a spreadsheet in the browser, however the spreadsheet itself uses HTML and JavaScript. Its possible to copy cells from excel and paste them into the JExcel table, you can then edit cells, add new cells etc.
You can then pass the data from the table to PHP using an AJAX call (JExcel provides methods to do this), im currently doing a project that does exactly this and it works very well. You can then do what you want with the saved data, e.g. insert it into a DB from PHP.
Hope this helps get you started.
PhpSpreadsheet is what you are looking for. There is another library which is dead, phpexcel, spreadsheet is a decendent of this.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a database on phpmyadmin. Then I have a website with some products. When user clicks on a specific product, he gets some details about that product. Those details are pulled from the database.
My question is: Is it possible to create a pdf file with the specific details about that product, so the user can download it?
So, the options i know are:
DOMPDF : php class that wrap the html and build the pdf. Works good, customizable, based on pdflib, if i remember right it takes even some CSS. Bad news: slow when the html is big or many complex.
this is a tutorial to follow :http://www.sitepoint.com/convert-html-to-pdf-with-dompdf/
HTML2PS: same of DOMPDF, but this one convert first in .ps (ghostscript), then, in whatever format you need (pdf, jpg, png). For me is little better then dompdf, but have the same speed problem.. oh, better compatibility with css.
Those two are php classes, but if you can install some software on the server, and access it throught passthru() or system(), give a look to these too:
wkhtmltopdf: based on webkit (safari's wrapper), is really fast and powerfull.. seem like is the best one (atm) for convert on the fly html pages to pdf, taking only 2 seconds for a 3 pages xHTML document with CSS2. Is a recent project, anyway, the google.code page is often updated.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want a simple html page that takes:
Name - user's time and date and set them in a single paged pdf then send it to a predefined email.
I am a newbie in php can you give me a simple example please?
You need external library. Click here, this pretty much give a good explanation on how to install and how to use the library.
Here is a good example
Use PDF_set_info to set information about the PDF and PDF_show for the actual content. Then if you wish to send it by email use the mail()function, click here for more information and examples.
FPDF is a great library for creating PDF Files. It is open-source (always a plus) and good for creating simple to complex PDF layouts.
You can also check out HTML2FPDF on sourceforge.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to display some pdf file on my website, but I do not want people to download the file. like scribd.com that displays the document.
anyone can help me?
If you want to make sure your user cannot download the original PDF file you will have to convert it to something else before.
Scribd converts the PDF to HTML (with one image in the background that contains all non-text objects). I am not aware of any PDF to HTML parsers, so you would have to write your own. Due to the nature of PDF files, this will unfortunately not be easy (see this question for some more details: Convert PDF to HTML in PHP?)
If you are fine with relying on some external web service, you might try this: https://cloudconvert.org/pdf-to-html
As an alternative to parsing the PDF to HTML, you could also just output it as an image. This is much easier to achieve but also not very nice in terms of user experience. If you chose this method, the easiest way would be to use ImageMagick and Ghostscript (see https://stackoverflow.com/a/467805):
<?php
$im = new imagick('file.pdf[0]');
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I used dompdf to download a receipt that is initially in HTML format. But as soon as it gets converted, the orientation and the format gets distorted. Is there way to download an HTML page as pdf with the same height and width as the HTML page?
No there is not. Every html->pdf tool are different. some doesnt allow css some allow html5 but very bad. some allow gif etc. lots of variations. You shouldnt look for a tool that render the html page and convert to pdf. You should find something like view the page and get its screenshots of it and print as pdf. because today's complex html pages are just too overwhelming for html->pdf tools. Those tools are made for html coders to create fancy pdfs.
Try to search browser plug-ins. If you find one you can automatize the process via linux konsole. (fire-fox, maybe chrome too not sure though)