php html_topdf,Converting HTML to PDF (not PDF to HTML) using PHP - Stack Overflow

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 2 years ago.

I am a PHP developer and in one of my projects, I need to convert some HTML documents (about 30 to 50 pages) into PDF documents.

My search has turned up the following possible solutions. Among them are some PHP libraries and some command line applications. Each has its own advantages and disadvantages.

PHP libraries:

fpdf (need more effort to convert)

tcpdf (need more effort to convert)

dompdf http://code.google.com/p/dompdf/ (compared to other, works well)

For each library, I have problems like:

Takes a long time (more than five minutes to convert 30 HTML pages)

Requires too many resources (memory and time)

(I set the following parameters in php.ini:

max_execution_time = 600

memory_limit = 250M

but things still don't work.)

Needs HTML pages to be well-formatted (e.g. no missing close tags)

All of these work when I try to convert simple HTML docs (five or fewer pages with little CSS)

Command line applications

All command line apps work perfectly and very quickly compared to the above libraries, but only when I run them directly on console. When I try to use them in PHP with exec() or system(), they give me errors.

The following are the command line applications and their errors when I run them in PHP:

html2pdf:11380): Gtk-WARNING **: cannot open display: :0.0

No protocol specified

wkhtmltopdf

Loading page: 10%

Loading page: 33%

Loading page: 100%

Waiting for redirect

Outputting pages

QPainter::begin(): Returned false

QPainter::begin(): Returned false

QPainter::save: Painter not active

QPainter::scale: Painter not active

QPainter::setRenderHint: Painter must be active to set rendering hints

QPainter::setBrush: Painter not active

QPainter::pen: Painter not active

QPainter::setPen: Painter not active

So now I am looking for help. Can anyone answer:

Which PHP library would work well in my case?

Why do these errors occur in command line applications?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue 3中,你可以使用html-to-image库将HTML元素转换为图像。首先,你需要安装html-to-image库,可以通过执行以下命令来完成安装: ``` npm install html-to-image ``` 接下来,在你的Vue组件中,你可以使用以下代码将HTML元素转换为图像: ```vue <template> <div> <div ref="elementToConvert"> <!-- 这里是你要转换为图像的HTML元素 --> <h1>Hello, World!</h1> <p>This is an example HTML element.</p> </div> <button @click="convertToImage">Convert to Image</button> </div> </template> <script> import htmlToImage from 'html-to-image'; export default { methods: { convertToImage() { const element = this.$refs.elementToConvert; htmlToImage.toPng(element) .then(function (dataUrl) { const link = document.createElement('a'); link.download = 'image.png'; link.href = dataUrl; link.click(); }) .catch(function (error) { console.error('Error converting HTML element to image:', error); }); } } } </script> ``` 在上面的代码中,我们首先在HTML中引入了一个包含要转换为图像的HTML元素的`div`。然后,我们在按钮的点击事件处理程序中调用`convertToImage`方法。该方法使用`html-to-image`库的`toPng`方法将HTML元素转换为PNG格式的图像。转换完成后,我们创建一个`<a>`元素,并将图像链接设置为下载链接,然后通过调用`click`方法触发下载。 记得将`elementToConvert`属性添加到你的组件的`ref`中,以便在`convertToImage`方法中引用该元素。 这样,当你点击"Convert to Image"按钮时,HTML元素将被转换为图像并下载到本地。你可以根据需要修改代码来适应你的实际场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值