Create pdf in Yii2 using mpdf extension: controller - view code - yii2

I have installed the kartik-v mpdf extension.
I want to display a pdf in Browser.
Can you consult over the code?
In controller I have the following:
use kartik\mpdf\Pdf;
.......
public function createPDF();
{
......
$mpdf = new Pdf(...);
$mpdf->SetTitle(...);
......
$mpdf->MultiCell(...);
.......
// display in browser
..........
?
..........
}
So, should I render a view through this function to display the pdf or I can do it directly from the controller?
Is there another way?
Thank you in advance

There is no need to render a seperate view for pdf. you can render pdf directly from controller. refer this example

Related

How to dynamically write the svg data to a file from an HTML page using JXBrowser as a background process

I am using JXBrowser and I have a valid license. I have a requirement to extract svg data from the HTML in the background using JXBrowser. HTML gets loaded from browser.loadURL(). When I print the HTML content , I see that all the data from ajax requests does not exist at all. SVG data is rendered using ajax request . I have also used the below method, but the response is same. Can anyone please help me to fix this issue ?
Browser.invokeAndWaitFinishLoadingMainFrame(browser, new Callback<Browser>() {
#Override
public void invoke(Browser browser) {
browser.loadURL("");
}
});

How can I generate a PDF of the current web page on button click?

How can I create and download a PDF version of the current web page (which is open in browser) on button click?
I am assuming you use jquery on client-side.
$('#button_id').click(function(){
window.print();
});
Which will guide you to print menu where you can choose "Save as PDF" which will save your current view as pdf.
Did it solve your issue?
You can look at Nuget package: Rotativa http://nuget.org/packages/Rotativa. It's based on wkhtmltopdf.
Usage is really simple.
Having an action you would like to serve as Pdf, instead of Html page. You can define an action that returns an ActionResult of the type ActionAsPdf (RouteAsPdf is also available). So the code is just:
public ActionResult PrintIndex()
{
return new ActionAsPdf("Index", new { name = "Giorgio" }) { FileName = "Test.pdf" };
}
With name = "Giorgio" being a route parameter.
Look at this GitHub
If i am getting you right. You want to generate a PDF from HMTL. Rotativa plugin is a good choice. Also, mentioned by Janmejay Kumar. If you want to implement this plugin. Please ask if you have any doubt .I have used this plugin for PDF and for image generation.

dompdf - just getting two pages in pdf

i want to print a html to a pdf. In the project i use symfony and twig to render the template. When i return the html to browser it's looking good.
And it should be about 10 pages.
But when i try to create the PDF, i just get two pages.
$html = $this->render('offer/print.html.twig', array('offer' => $Offer ));
$options = new Options();
$options->setIsRemoteEnabled( false );
// instantiate and use the dompdf class
$dompdf = new Dompdf( $options );
$dompdf->setPaper('A4');
// Render the HTML as PDF
$dompdf->loadHtml( $html );
$dompdf->render();
$dompdf->stream();
PHP 7
Symfony 3.0.9
dompdf 0.7.0
It doesn't looks like the html in the browser. I have to debug the css later.
Can anyone tell me why i just getting two pages in the pdf?
interesting it seems the problem is the render() function.
When i use renderView() instead i got my output over multiple pages.
render() also returns the HTTP Headers. Out of this reason i got a broken pdf with the header information on the first side and all content on the second.
Just the CSS isn't working but this is another issue :)
Please upload you twig file. I think the issues with you twig css.

How to have multiple HTML static pages on a ASP.NET MVC project using the Layout page?

On my MVC project I have to incorporate 40 static pages.
I want these pages to use the Layout page.
What is the best way to do that?
I know this question was asked before but I didn't find any good answer.
Any advise?
I don't relly know ASP, but I try to give a generic answer.
So I think if you have a lot of similar static pages, somehow you could make a controller action that handles all these pages. For example the action gets the name of the page as a path variable in the URL, and return the view according to that.
But if that is not possible in the language you are using, you can just make simple separate actions for these pages. Maybe you could group the related ones into the same controller, so you would have a few controllers that handle these pages, and they are not stuffed in one controller.
Basically the solution is very simple, you have to create views for you static HTML (cshtml), then you should add a Route to your Route.Config like this:
routes.MapRoute(
"OrdeForm",
"OrderForm/{file}",
new { controller = "MyController", action = "Page", file} = "" }
);
Where "File" is a dynamic parameter that gets the View name from the URL and renders the right View.
The global controller should be something like this:
public class OrderFormController : Controller
{
public ActionResult Index(string file)
{
return View(file);
}
}
That works perfectly!
Thank you #Erik Philips for the excellant answer!

changing style on generating pdf with Primefaces dataExporter

I'm using Primefaces dataExporter to generate pdf from a dataTable. The pdf generated has all the columns with the same width. I'm looking for a way to change the style of the table on the postProcessor/preProcessor functions. Can I use the setHtmlStyleClass method to change something before generating pdf? I tried to use it, but with no success. I think I didnt understand it correctly.
public void preProcessPDF(Object document) throws IOException, BadElementException, DocumentException {
Document pdf = (Document) document;
pdf.setHtmlStyleClass("reportClass");
...
}
If I can use that method, where can I define reportClass ? Is it a css class for the page on the browser?
If you look at whats going on in the PDFExporter.java export method, the data table in the PDF can not manipulated.
First a com.itextpdf.text.Document object is created.
Document document = new Document(PageSize.A4.rotate());
Then the preProcessor method is called passing the Document, this is before the table is added to the PDF Document.
if(preProcessor != null) {
preProcessor.invoke(facesContext.getELContext(), new Object[]{document});
}
Then the com.itextpdf.text.pdf.PdfPTable is created. The exportPDFTable method doesn't do any special formatting.
PdfPTable pdfTable = exportPDFTable(table, excludeColumns);
document.add(pdfTable);
Now the postProcess method is called and the Document is passed again. Here I would think you would be able to access and change the PdfPTable from the Document object but looking at the iText api it doesn't look like you can.
if(postProcessor != null) {
postProcessor.invoke(facesContext.getELContext(), new Object[]{document});
}
So if you want a styled PDF table your going to have to implement your own PDF export. Hopefully looking at how the PrimeFaces PDFExporter is done will help you with that.