I want to convert html display contents to PDF

Asked 2 years ago, Updated 2 years ago, 112 views

I'd like to convert the HTML display I'm currently displaying to PDF.

Even if you try using PHP's fpdf, it seems impossible unless you declare the specified text or cells in PHP.
(Then it takes a lot of time and on the contrary, it's meaningless...)

How can I convert what I'm viewing into PDF with one click?
If it's possible on the Javascript side, I don't care how to do it, thank you.

● fpdf official page
http://www.fpdf.org/

=======================Addition=========================

The pdf was successful in mpdf.
I'd like to pdf the page generated by PHP, but in that case, it's still running. Unable to display.
I'd like to convert the displayed page, is there any way?

javascript php pdf

2022-09-30 19:17

2 Answers

As for Javascript, the library jsPDF seems to support PDF generation from DOM elements.However, if you look at HTML Renderer in the sample on the site, it seems that CSS does not apply.

You can use html2canvas, etc. to convert it into an image and embed it in a PDF.

For PHP, there are ways to use PHP's library from HTML analysis, such as dompdf and tcpdf, and PhantomJS and wkhtmltopdf using browser rendering engines.In the latter case, the program must be installed separately from PHP, so it may be difficult for rental servers.


2022-09-30 19:17

You can convert from Html to PDF in Java, and you can easily complete it with components such as Spire.PDF for Java.

import com.spire.pdf.graphics.PdfMargins;
import com.spire.pdf.htmlconverter.qt.HtmlConverter;
import com.spire.pdf.htmlconverter.qt.Size;

public class HtmltoPDF
{
    public static void main (String[]args)
    {
        // define the HTML link and result PDF
        String url="https://www.xxxxxxx.com/";
        String fileName = "Result.pdf";
        // Set the plugin path
        String pluginPath="D:/Qt/plugins_32";
        HtmlConverter.setPluginPath(pluginPath);
        // convert HTML to PDF and set the size of the result PDF page
        HtmlConverter.convert(url,fileName,true,1000000,newSize(600f,900f),new PdfMargins(0));
    }
}

That's all you need, and you can convert other formats.


2022-09-30 19:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.