Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách in trang web hoặc in một khu vực cụ thể trong trang web bằng JavaScript
Có nhiều plugin để in trang web nhưng trong hướng dẫn này, chúng tôi sẽ in trang web mà không cần bất kỳ plugin nào
Bạn cũng có thể thích chụp ảnh màn hình trang web bằng JavaScript
Tham gia với hơn 27.000 thành viên trong nhóm Google của bạn và nhận các hướng dẫn mới nhất
Nhận các hướng dẫn mới nhất của chúng tôi, Hướng dẫn cách thực hiện về phát triển web mỗi ngày ngay trong hộp thư đến của bạn
HÃY XEM THỬ HƯỚNG DẪN TRỰC TIẾP NÀY →
Để in trang web chỉ cần một bước. -
- Tạo một tệp HTML và xác định đánh dấu và viết kịch bản
Bước 1. Tạo một tệp HTML và xác định đánh dấu và viết kịch bản
Chúng tôi tạo một tệp HTML và lưu nó với tên in. html
Print Webpage Using JavaScript
Some sample content and this is a demo to print webpage using javascript Some sample content and this is a demo to print webpage using javascript Some sample content and this is a demo to print webpage using javascript Some sample content and this is a demo to print webpage using javascript Some sample content and this is a demo to print webpage using javascript Some sample content and this is a demo to print webpage using javascript
Trong bước này, chúng tôi tạo một div mẫu và chèn một số nội dung mẫu để in và chúng tôi tạo hai nút, một nút được sử dụng để in toàn bộ trang web và một nút khác được sử dụng để in 'print_div'
Chúng tôi đã tạo hai chức năng để in trang web và 'print_div' cả hai chức năng đều sử dụng cùng một phương thức để in nội dung
Bạn cũng có thể muốn thêm trình kiểm tra chính tả vào trang web của mình bằng jQuery
Đó là tất cả, đây là cách in trang web bằng JavaScript. Bạn có thể tùy chỉnh thêm mã này theo yêu cầu của bạn. Và xin vui lòng cho nhận xét về hướng dẫn này
• Thêm một Bộ điều khiển mới vào dự án của bạn và đặt tên là WebClientPrintController. php. Sau đó sao chép/dán đoạn mã sau
has[WebClientPrint::SID]]{
$sid = $request->input[WebClientPrint::SID];
}
try{
//get query string from url
$query = substr[$request->fullUrl[], strpos[$request->fullUrl[], '?']+1];
//get request type
$reqType = WebClientPrint::GetProcessRequestType[$query];
if[$reqType == WebClientPrint::GenPrintScript ||
$reqType == WebClientPrint::GenWcppDetectScript]{
//Let WebClientPrint to generate the requested script
//Get Absolute URL of this file
$currentAbsoluteURL = substr[$request->fullUrl[], 0, strrpos[$request->fullUrl[], '?']];
// Return WebClientPrint's javascript code
return response[WebClientPrint::generateScript[$currentAbsoluteURL, $query]]
->header['Content-Type', 'text/javascript'];
}
else if [$reqType == WebClientPrint::ClientSetWcppVersion]
{
//This request is a ping from the WCPP utility
//so store the session ID indicating this user has the WCPP installed
//also store the WCPP Version if available
if[$request->has[WebClientPrint::WCPP_SET_VERSION] && strlen[$request->input[WebClientPrint::WCPP_SET_VERSION]] > 0]{
WebClientPrint::cacheAdd[$sid, WebClientPrint::WCP_CACHE_WCPP_VER, $request->input[WebClientPrint::WCPP_SET_VERSION]];
}
return;
}
else if [$reqType == WebClientPrint::ClientSetInstalledPrinters]
{
//WCPP Utility is sending the installed printers at client side
//so store this info with the specified session ID
WebClientPrint::cacheAdd[$sid, WebClientPrint::WCP_CACHE_PRINTERS, strlen[$request->input[WebClientPrint::WCPP_SET_PRINTERS]] > 0 ? $request->input[WebClientPrint::WCPP_SET_PRINTERS] : ''];
return;
}
else if [$reqType == WebClientPrint::ClientSetInstalledPrintersInfo]
{
//WCPP Utility is sending the installed printers at client side with detailed info
//so store this info with the specified session ID
//Printers Info is in JSON format
$printersInfo = $request->input['printersInfoContent'];
WebClientPrint::cacheAdd[$sid, WebClientPrint::WCP_CACHE_PRINTERSINFO, $printersInfo];
return;
}
else if [$reqType == WebClientPrint::ClientGetWcppVersion]
{
//return the WCPP version for the specified Session ID [sid] if any
return response[WebClientPrint::cacheGet[$sid, WebClientPrint::WCP_CACHE_WCPP_VER]]
->header['Content-Type', 'text/plain'];
}
else if [$reqType == WebClientPrint::ClientGetInstalledPrinters]
{
//return the installed printers for the specified Session ID [sid] if any
return response[base64_decode[WebClientPrint::cacheGet[$sid, WebClientPrint::WCP_CACHE_PRINTERS]]]
->header['Content-Type', 'text/plain'];
}
else if [$reqType == WebClientPrint::ClientGetInstalledPrintersInfo]
{
//return the installed printers with detailed info for the specified Session ID [sid] if any
return response[base64_decode[WebClientPrint::cacheGet[$sid, WebClientPrint::WCP_CACHE_PRINTERSINFO]]]
->header['Content-Type', 'text/plain'];
}
}
catch [Exception $ex]
{
throw $ex;
}
}
}
• Loại trừ WebClientPrintAPIController & StoreImageFileController [chúng tôi sẽ tạo nó sau] khỏi CSRF Protection bằng cách chỉnh sửa app\Http\Middleware\VerifyCsrfToken. php để nó trông giống như đoạn mã sau