Home > Net >  Wkhtmltopdf turn PDF page
Wkhtmltopdf turn PDF page

Time:11-25

Dynamic web pages with background images and pictures, now can only turn a text content,

CodePudding user response:

Turn to the great god, nasty, code is as follows:

CodePudding user response:

reference 1st floor qq_33035717 response:
turn to the great god, and the code is as follows:

Have, have one answer me ahhh

CodePudding user response:

String fileNameWithOutExtention=HttpContext. Current. Server. MapPath (".. PDF \ \ Content \ "+ Guid NewGuid () +". The PDF ");
String wkhtmltopdfPath=HttpContext. Current. Server. MapPath (".. \ App_Start \ wkhtmltopdf exe ");
If (string. IsNullOrEmpty (HTML))
{
Return "false";
}
ProcessStartInfo si;
The StringBuilder paramsBuilder=new StringBuilder ();
ParamsBuilder. Append (" - page - size A4 ");
ParamsBuilder. AppendFormat ({0} '\' \ "" {1} \ ""," - ", fileNameWithOutExtention);

Si=new ProcessStartInfo ();
Si. CreateNoWindow=true;
Si. The FileName=wkhtmltopdfPath;
Si. The Arguments=paramsBuilder. ToString (); Si. UseShellExecute=false; Si. RedirectStandardError=true; Si. RedirectStandardInput=true;
Using (var process=new process ())
{
Process. StartInfo=si; Process. The Start ();
Using (var stream=process. StandardInput) {
Byte [] buffer.=Encoding UTF8. GetBytes (HTML); Stream. The BaseStream. Write (buffer, 0, buffer. Length); Stream. WriteLine ();
}
Process. WaitForExit ();
}
If (the File. The Exists (fileNameWithOutExtention)) {
//the file read into the document flow
FileStream fs=new FileStream (fileNameWithOutExtention, FileMode. Open);
Byte [] file=new byte [fs. Length];
Fs. Read (file, 0, the file Length);
Fs. The Close ();
}
The else {throw new Exception (" file does not exist!" ); }
Return fileNameWithOutExtention;

CodePudding user response:

the building Lord teach me

CodePudding user response:

Cloud nine document conversion interface, support word to PDF, PDF pages, PPT to PDF, HTML to PDF, HTML to PDF in a variety of formats such as document conversion, the original format and layout, vector scaling, can be easily simple call online conversion, want to can go to the website to see nine cloud experience

CodePudding user response:

Cef is saved as a PDF directly

CodePudding user response:

Package com. HtmlToPdf. Cn;

Import the Java. IO. BufferedReader;
Import the Java. IO. The File;
import java.io.IOException;
Import the Java. IO. InputStreamReader;
Import the Java. Nio. Charset. Charset;
Import the Java. Util. UUID;

The class wkthmltopdf {
//wkhtmltopdf in the system path
//private static final String toPdfTool="D: \ \ wkhtmltox 0.12.5-1. Mxe - cross - win64 \ \ bin \ \ wkhtmltopdf exe";

/* *
* HTML to PDF
*
* @ param srcPath
* HTML path, can is the path of the hard disk, network path
* @ param destPath
* PDF save the path
* @ return transformation successfully returns true
*/
Public static Boolean convert (String srcPath, String destPath) throws IOException {
Process the ps=Runtime. GetRuntime (). The exec (" where wkhtmltopdf. Exe ");
BufferedReader br=null;
Br=new BufferedReader (new InputStreamReader (ps. GetInputStream (), Charset. Class.forname (" GBK ")));
//wkhtmltopdf in the system path
String path="";
String text="";

While ((text=br. ReadLine ())!=null) {
Path=text;
}
The File File=new File (destPath);
The File parent=File. GetParentFile ();
//if PDF save path does not exist, then create a path
if (! The parent. The exists ()) {
//true representative has read and write access
File. SetWritable (true);
The parent. The file.mkdirs ();
}
The StringBuilder CMD=new StringBuilder ();
if (! System. GetProperty (" OS. The name "). The contains (" Windows ")) {
//the Windows
//toPdfTool=FileUtil. ConvertSystemFilePath ("/home/ubuntu/wkhtmltox/bin/wkhtmltopdf ");
}
CMD. Append (path);
CMD. Append (" ");
//below the header lines
CMD. Append (" -- the header - line ");
//set on the page margins
CMD. Append (" - margin - the top 3 cm ");
//set the header and content of distance, the default 0)
CMD. Append (" -- the header - spacing 5 ");
//set in the center of the footer content
CMD. Append (" - footer - the center of the first [page]/[topage] total pages ");
//* shows a line on the footer content)
CMD. Append (" -- footer - line ");
//set the footer and the content of distance)
CMD. Append (" - footer - spacing 5 ");
//want to convert the URL address of the
CMD. Append (srcPath);
CMD. Append (" ");
//PDF store path
CMD. Append (destPath);
A Boolean result=true;
Try {
Process the proc=Runtime. GetRuntime (). The exec (CMD) toString ());
HtmlToPdfInterceptor error=new HtmlToPdfInterceptor (proc. GetErrorStream ());
HtmlToPdfInterceptor output=new HtmlToPdfInterceptor (proc. GetInputStream ());
Error. The start ();
The output. The start ();
Proc. WaitFor ();
} the catch (Exception e) {
PDF conversion System. The out. Println (" error ");
Result=false;
e.printStackTrace();
}

return result;
}

Public static void main (String [] args) throws IOException {
Wkthmltopdf. Convert (" https://www.baidu.com ", "D: \ \ test201905 \ \ 13. PDF");

}
}
Close test available
  •  Tags:  
  • C#
  • Related