Export PDF using Phantomjs

Source: Internet
Author: User
Use Phantomjs to export the PDF file to a new requirement and export the electronic protocol (PDF) to the user. Because we use PHP in the background, we naturally look for PHP solutions. I read several libraries, including tens of thousands and tens of thousands of downloaded Packagist libraries. Alas, I have to say that although PHP is the best language in the world, PHP developers have a true aesthetic level, the nice thing to say is that you can't watch it ......

The second problem is that many PHP files need to be "spelled" in PDF, with one line, one element and one element, and a complete document is assembled. It is quite laborious, and style is not easy to control. Conversion from HTML also has a similar problem. After studying it for a while, I thought it was getting worse and worse. Alas, forget it. change to Phantomjs.

Phantomjs is a command line Webkit tool. we can understand it as a browser that does not output pages, but it supports various functions of the browser, because it has Webkit. Therefore, rendering webpages and capturing images is just a piece of cake.

It is very easy to output PDF with Phantomjs:

  • First, set the width and height (for convenient printing, our electronic protocols should be paginated and have a page header and footer) to complete the page template.
  • Complete the Phantomjs script. Because you only use it to generate documents, you do not need Web services.
  • Use PHP to call the script, generate a PDF file, and then download readfile to the user.

The advantage of doing so is that we can preview the effect at any time. HTML is easy to read and modify, and PHP is also very convenient to replace the content. In addition, the code is very simple. combined with official examples, it will soon be written:

'use strict';var page = require('webpage').create()  , system = require('system')  , args = system.args  , url = args.length > 1 ? args[1] : 'http://www.dianjoy.com/'  , filename = args.length > 2 ? args[2] : 'tmp';page.viewportSize = {  width: 800,  height: 1100};url = decodeURIComponent(url);page.open(url, function (status) {  console.log(status);  if (status === 'success') {    page.render('/tmp/pdf/' + filename + '.pdf');  }  phantom.exit();});

The biggest problem with deploying this code is that GFW causes npm install phantomjs-g to fail and direct download of zip files (because it is stored on Amazon S3 ). As a result, he continued to cheer for the disease and killed the principal as soon as possible, as well as other wall builders.

The second problem is that PHP cannot execute scripts. Reading only the document is simple:

exec('/usr/local/phantomjs/bin/phantomjs pdf.js http://meathill.com/ meathill');

But in fact, there was neither a document nor any response. debugging lasted for half a day. I suddenly remembered that I encountered a similar problem when I used Apktool to parse the installation package, so I added 2> & 1 at the end to solve the problem.

Google doesn't know much about it either. Let's talk about it later.

Other references:

Shell_exec

Text is irrelevant. In fact, it was a friend of the University who was overjoyed today and put a strange picture of her to congratulate her.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.