安裝php-phantomjs

文檔介紹

https://jonnnnyw.github.io/ph...
有一個比較坑爹的地方就是點擊裏面的介紹會自動變成http開頭的網址可能會打不開,只須要填個s變成https便可打開。php

首先要安裝composer

安裝composer中文鏡像http://www.phpcomposer.com/,而後重啓git

建立composer.json

{
        "scripts": {
            "post-install-cmd": [
                "PhantomInstaller\\Installer::installPhantomJS"
            ],
            "post-update-cmd": [
                "PhantomInstaller\\Installer::installPhantomJS"
            ]
        },
        "config": {
            "bin-dir": "bin"
        },
        "require": {
                "jonnyw/php-phantomjs": "4.*"
        }
}

而後安裝

進入項目文件夾運行下面的的命令便可 composer require "jonnyw/php-phantomjs:4.*"github

標題文字

完成後會出現下面的文件夾

注:還會有一個bin文件夾,裏面是phantomjs.exe。個人沒有是由於提早安裝了。json

運行測試代碼

<?php

    require "vendor\autoload.php";
    use JonnyW\PhantomJs\Client;
    // require_once 'vendor_phantomjs/jonnyw/php-phantomjs/src/JonnyW/PhantomJs/Client.php';

    $client = Client::getInstance();
    $client->getEngine()->setPath('E:/xampp/php/phantomjs.exe');
    //上面一行要填寫本身的phantomjs路徑
    /** 
     * @see JonnyW\PhantomJs\Http\PdfRequest
     **/
    $delay = 7;
    $request = $client->getMessageFactory()->createPdfRequest('https://www.baidu.com/', 'GET', 5000); //參數裏面的數字5000是網頁加載的超時時間,放在網絡問題一直加載,能夠不填寫,默認5s。
    $request->setOutputFile('E:/document.pdf');
    $request->setFormat('A4');
    $request->setOrientation('landscape');
    $request->setMargin('1cm');
    $request->setDelay($delay);//設置delay是由於有一些特效會在頁面加載完成後加載,沒有等待就會漏掉

    /** 
     * @see JonnyW\PhantomJs\Http\Response 
     **/
    $response = $client->getMessageFactory()->createResponse();

    // Send the request
    $client->send($request, $response);

謝謝觀看

相關文章
相關標籤/搜索