Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
about QM3 Some suggestions
#5
3.
I found a piece of code: url2pdf

Download:
https://wkhtmltopdf.org/downloads.html

C# code:
public void wkhtmltopdf(string filePath, string fileName, string url)
        {
            Process p = new Process();
            //wkhtmltopdf The path where the plug-in is installed
            string dllstr = AppDomain.CurrentDomain.BaseDirectory + "wkhtmltopdf\\wkhtmltopdf.exe";
            //string dllstr = "C:\\Windows\\System32\\wkhtmltopdf.exe";
            if (System.IO.File.Exists(dllstr))
            {
                string savepath = Path.Combine(filePath, fileName);
                p.StartInfo.FileName = dllstr;

                StringBuilder paramsBuilder = new StringBuilder();
                paramsBuilder.Append("--page-width " + txt_width.Text + "mm ");
                paramsBuilder.Append("--zoom 1.2 ");
                paramsBuilder.Append("--disable-smart-shrinking ");
                paramsBuilder.Append("--page-height " + txt_height.Text + "mm ");
                paramsBuilder.Append("--margin-bottom 0mm ");
                paramsBuilder.Append("--margin-left 0mm ");
                paramsBuilder.Append("--margin-right 0mm ");
                paramsBuilder.Append("--margin-top 0mm ");
                paramsBuilder.AppendFormat("\"{0}\" \"{1}\"", url, savepath);
                p.StartInfo.Arguments = paramsBuilder.ToString();
                //p.StartInfo.Arguments = " \"" + url + "\"  \"" + savepath + "\"";

                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = true;

                p.Start();
                p.WaitForExit();
            }
        }

Another way

nuget: TuesPechkin
reference TuesPechkin.dll

C# code:
/// <summary>
        /// Convert a web page to a pdf file
        /// </summary>
        /// <param name="filePath">File path/directory</param>
        /// <param name="fileName">filename</param>
        /// <param name="url">Web page URL</param>
        public bool ConvertWebpageToPDF(string filePath, string fileName, string url)
        {
            if (Directory.Exists(filePath) == false)
                Directory.CreateDirectory(filePath);
            try
            {
                HtmlToPdfDocument htmlToPdfDocument = new HtmlToPdfDocument();
                GlobalSettings globalSettings = new GlobalSettings();
                globalSettings.ProduceOutline = new bool?(true);

                //html The width and height of the web page content
                double width = 100; double.TryParse(txt_width.Text, out width);
                double height = 120; double.TryParse(txt_height.Text, out height);

                //Set the pdf width and height
                double proportion = 1.26;
                width /= proportion;
                height = Math.Floor(height / proportion);
                string Width = width.ToString(); string Height = height.ToString();
                globalSettings.PaperSize = new PechkinPaperSize(Width, Height);

                //Sets the margins around the pdf document
                globalSettings.Margins.Top = 0;
                globalSettings.Margins.Right = 0;
                globalSettings.Margins.Bottom = 0;
                globalSettings.Margins.Left = 0;
                globalSettings.Margins.Unit = Unit.Centimeters;
                string pageUrl = url;
                ObjectSettings objectSettings = new ObjectSettings();

                double value = 1;
                objectSettings.HtmlText = string.Empty;
                objectSettings.PageUrl = pageUrl;
                objectSettings.LoadSettings.BlockLocalFileAccess = true;
                objectSettings.LoadSettings.ZoomFactor = new double?(value);
                objectSettings.WebSettings.PrintMediaType = new bool?(true);
                objectSettings.WebSettings.PrintBackground = new bool?(false);
                htmlToPdfDocument.GlobalSettings = globalSettings;
                htmlToPdfDocument.Objects.Add(objectSettings);
                IPechkin pechkin = Factory.Create();
                byte[] array = pechkin.Convert(htmlToPdfDocument);
                string path = string.Empty;
                if (array == null)
                {
                    return false;
                }

                path = Path.Combine(filePath, fileName);
                using (FileStream fileStream = new FileStream(path, FileMode.Create))
                {
                    fileStream.Write(array, 0, array.Length);
                    fileStream.Flush();
                    fileStream.Close();

                    FileStream fs = new FileStream(path, FileMode.Open);
                    byte[] file = new byte[fs.Length];
                    fs.Read(file, 0, file.Length);
                    fs.Close();
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }


Messages In This Thread
about QM3 Some suggestions - by Davider - 08-14-2022, 08:45 AM
RE: about QM3 Some suggestions - by Gintaras - 08-14-2022, 11:05 AM
RE: about QM3 Some suggestions - by Davider - 08-14-2022, 11:14 AM
RE: about QM3 Some suggestions - by Gintaras - 08-14-2022, 11:37 AM
RE: about QM3 Some suggestions - by Davider - 08-14-2022, 10:52 PM
RE: about QM3 Some suggestions - by Gintaras - 08-15-2022, 03:26 AM
RE: about QM3 Some suggestions - by Davider - 08-15-2022, 11:23 PM
RE: about QM3 Some suggestions - by Gintaras - 08-16-2022, 04:28 AM
RE: about QM3 Some suggestions - by Gintaras - 11-11-2022, 08:59 AM
RE: about QM3 Some suggestions - by Davider - 11-12-2022, 01:55 AM
RE: about QM3 Some suggestions - by Gintaras - 11-12-2022, 08:15 AM
RE: about QM3 Some suggestions - by Davider - 11-12-2022, 10:17 AM
RE: about QM3 Some suggestions - by Davider - 05-20-2023, 10:35 PM
RE: about QM3 Some suggestions - by Gintaras - 05-21-2023, 04:28 AM
RE: about QM3 Some suggestions - by Davider - 06-20-2023, 03:37 AM
RE: about QM3 Some suggestions - by Davider - 06-20-2023, 10:04 AM
RE: about QM3 Some suggestions - by Davider - 06-21-2023, 03:15 PM
RE: about QM3 Some suggestions - by Davider - 06-21-2023, 11:26 PM
RE: about QM3 Some suggestions - by Gintaras - 06-22-2023, 05:48 AM
RE: about QM3 Some suggestions - by Davider - 06-22-2023, 09:09 AM
RE: about QM3 Some suggestions - by Gintaras - 06-22-2023, 09:34 AM
RE: about QM3 Some suggestions - by Davider - 06-22-2023, 09:40 AM
RE: about QM3 Some suggestions - by Davider - 06-22-2023, 11:00 AM
RE: about QM3 Some suggestions - by Davider - 06-23-2023, 02:45 AM
RE: about QM3 Some suggestions - by Gintaras - 06-23-2023, 03:32 AM
RE: about QM3 Some suggestions - by Gintaras - 06-23-2023, 10:46 AM
RE: about QM3 Some suggestions - by Davider - 06-23-2023, 01:28 PM
RE: about QM3 Some suggestions - by Gintaras - 06-23-2023, 05:45 PM
RE: about QM3 Some suggestions - by Davider - 06-23-2023, 10:25 PM
RE: about QM3 Some suggestions - by Davider - 06-23-2023, 11:32 PM
RE: about QM3 Some suggestions - by Gintaras - 06-24-2023, 05:15 AM
RE: about QM3 Some suggestions - by Davider - 06-24-2023, 07:20 AM
RE: about QM3 Some suggestions - by Davider - 06-25-2023, 12:23 PM
RE: about QM3 Some suggestions - by Davider - 07-11-2023, 12:48 AM
RE: about QM3 Some suggestions - by Davider - 07-14-2023, 11:04 AM
RE: about QM3 Some suggestions - by Gintaras - 07-14-2023, 11:17 AM
RE: about QM3 Some suggestions - by Gintaras - 07-14-2023, 11:25 AM
RE: about QM3 Some suggestions - by Davider - 07-14-2023, 12:07 PM
RE: about QM3 Some suggestions - by Davider - 07-14-2023, 11:24 PM
RE: about QM3 Some suggestions - by Davider - 07-26-2023, 09:18 AM
RE: about QM3 Some suggestions - by Gintaras - 07-26-2023, 10:01 AM
RE: about QM3 Some suggestions - by Davider - 07-26-2023, 12:45 PM
RE: about QM3 Some suggestions - by Gintaras - 07-26-2023, 01:09 PM
RE: about QM3 Some suggestions - by Davider - 07-31-2023, 04:35 AM
RE: about QM3 Some suggestions - by Gintaras - 07-31-2023, 08:28 AM
RE: about QM3 Some suggestions - by Davider - 08-01-2023, 01:12 PM
RE: about QM3 Some suggestions - by Gintaras - 08-01-2023, 06:42 PM
RE: about QM3 Some suggestions - by Davider - 08-03-2023, 09:44 PM
RE: about QM3 Some suggestions - by Davider - 01-26-2024, 11:09 PM
RE: about QM3 Some suggestions - by Davider - 01-27-2024, 01:29 AM
RE: about QM3 Some suggestions - by Davider - 01-29-2024, 05:40 AM
RE: about QM3 Some suggestions - by Gintaras - 01-29-2024, 09:30 AM
RE: about QM3 Some suggestions - by Davider - 01-29-2024, 10:54 AM
RE: about QM3 Some suggestions - by Gintaras - 01-29-2024, 12:54 PM
RE: about QM3 Some suggestions - by Davider - 01-29-2024, 01:44 PM
RE: about QM3 Some suggestions - by Gintaras - 01-29-2024, 02:04 PM
RE: about QM3 Some suggestions - by Davider - 01-30-2024, 01:19 AM
RE: about QM3 Some suggestions - by Davider - 02-02-2024, 12:15 AM
RE: about QM3 Some suggestions - by Gintaras - 02-02-2024, 05:41 AM
RE: about QM3 Some suggestions - by Gintaras - 02-08-2024, 02:37 PM
RE: about QM3 Some suggestions - by Davider - 02-09-2024, 11:36 AM
RE: about QM3 Some suggestions - by Gintaras - 02-09-2024, 12:06 PM
RE: about QM3 Some suggestions - by Davider - 02-09-2024, 12:48 PM
RE: about QM3 Some suggestions - by Gintaras - 02-10-2024, 09:08 AM
RE: about QM3 Some suggestions - by Davider - 02-29-2024, 01:14 PM
RE: about QM3 Some suggestions - by Davider - 03-02-2024, 06:30 AM
RE: about QM3 Some suggestions - by Gintaras - 03-02-2024, 06:36 AM
RE: about QM3 Some suggestions - by Davider - 03-02-2024, 06:37 AM
RE: about QM3 Some suggestions - by Davider - 03-04-2024, 11:15 PM
RE: about QM3 Some suggestions - by Davider - 03-14-2024, 03:19 AM
RE: about QM3 Some suggestions - by Gintaras - 03-14-2024, 04:43 AM
RE: about QM3 Some suggestions - by Davider - 03-14-2024, 11:57 PM
RE: about QM3 Some suggestions - by Gintaras - 03-16-2024, 05:17 AM
RE: about QM3 Some suggestions - by Davider - 03-18-2024, 01:40 PM
RE: about QM3 Some suggestions - by Davider - 03-19-2024, 01:30 AM
RE: about QM3 Some suggestions - by Gintaras - 03-19-2024, 09:45 AM
RE: about QM3 Some suggestions - by Davider - 03-19-2024, 09:53 AM
RE: about QM3 Some suggestions - by Gintaras - 03-19-2024, 10:00 AM
RE: about QM3 Some suggestions - by Gintaras - 03-19-2024, 01:13 PM
RE: about QM3 Some suggestions - by Davider - 03-19-2024, 02:28 PM
RE: about QM3 Some suggestions - by Davider - 03-20-2024, 02:26 AM
RE: about QM3 Some suggestions - by Gintaras - 03-20-2024, 08:24 AM
RE: about QM3 Some suggestions - by Davider - 03-20-2024, 09:09 AM
RE: about QM3 Some suggestions - by Davider - 03-20-2024, 11:13 PM
RE: about QM3 Some suggestions - by Gintaras - 03-21-2024, 04:43 AM
RE: about QM3 Some suggestions - by Davider - 03-23-2024, 01:33 PM
RE: about QM3 Some suggestions - by Davider - 03-23-2024, 02:43 PM
RE: about QM3 Some suggestions - by Gintaras - 03-23-2024, 02:48 PM
RE: about QM3 Some suggestions - by Davider - 03-23-2024, 03:42 PM
RE: about QM3 Some suggestions - by Davider - 03-24-2024, 08:47 AM
RE: about QM3 Some suggestions - by Gintaras - 03-24-2024, 08:50 AM
RE: about QM3 Some suggestions - by Davider - 03-28-2024, 10:11 AM
RE: about QM3 Some suggestions - by Davider - 04-18-2024, 10:15 PM
RE: about QM3 Some suggestions - by Gintaras - 04-19-2024, 03:49 AM
RE: about QM3 Some suggestions - by Davider - 04-23-2024, 08:55 AM
RE: about QM3 Some suggestions - by Davider - 05-10-2024, 01:57 AM
RE: about QM3 Some suggestions - by Gintaras - 05-10-2024, 03:52 AM
RE: about QM3 Some suggestions - by Davider - 05-16-2024, 01:45 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)