i want to upload the text files to the server by converting them into base64
string from android
side and send them to the server using asp.net
web api i again decode them in asp.net
web api ..but before actually storing them into my directory i want to convert the files into a pdf document whether they were .docx
.txt
or of any other type
i have already tried the following code but it gives the error object reference not set to an instance of an object i don't know how to solve it and what are the issues
private static string saveFile(long id, string base64String) { try {
String path = HttpContext.Current.Server.MapPath("~/images"); //Path
//Check if directory exist
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path); //Create directory if it doesn't exist
}
string imageName = "Documentation" + id + ".docx";
//set the image path
string imgPath = Path.Combine(path, imageName);
byte[] imageBytes = Convert.FromBase64String(base64String);
File.WriteAllBytes(imgPath, imageBytes);
Word2Pdf objWordPDF = new Word2Pdf();
object FromLocation = path+"\\"+imageName;
string FileExtension = Path.GetExtension(imageName);
string ChangeExtension = imageName.Replace(FileExtension, ".pdf");
if (FileExtension == ".doc" || FileExtension == ".docx")
{
object ToLocation = path + "\\" + ChangeExtension;
objWordPDF.InputLocation = FromLocation;
objWordPDF.OutputLocation = ToLocation;
objWordPDF.Word2PdfCOnversion();
}
return imageName;
}
catch (Exception ex)
{
return null;
}
}
this code is not producing the desired result it returns a null as image name
I don't actually have a solid answer but in my experience i once did this using wkhtmltopdf tool wich is an outside tool to convert webpages to pdf. the logic goes as the need to render whatever u want as pdf into an view and use the service of wkhtmltopdf to convert it.
for the service here is the sample i have :
ServicePDF.cs :
public class ServicePdf : IServicePdf
{
private readonly IConfiguration _configuration;
public ServicePdf(IConfiguration configuration)
{
_configuration = configuration;
}
public PdfGlobalOptions Options { set; get; }
/// <summary>
/// Pertmet la génération d'un fichier PDF
/// </summary>
/// <param name="url">l'url à convertir exp : http://www.google.fr</param>
/// <param name = "filename" > Nom du fichier pdf en output</param>
/// <returns>byte[] pdf en cas de succès, le message d'erreur en cas d'echec</returns>
public object GeneratePDF(string url, string filename)
{
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = _configuration.GetSection("wkhtmltopdf").GetSection("Path").Value + _configuration.GetSection("wkhtmltopdf").GetSection("Program").Value;
StringBuilder arguments = new StringBuilder();
arguments.Append("--page-size ");
arguments.Append("A4");
arguments.Append(" ");
if (Options.Orientation != PdfOrientation.Portrait)
{
arguments.Append("--orientation Landscape");
arguments.Append(" ");
}
if (Options.MarginLeft.HasValue)
{
arguments.Append("--margin-left ");
arguments.Append(Options.MarginLeft);
arguments.Append(" ");
}
if (Options.MarginTop.HasValue)
{
arguments.Append("--margin-top ");
arguments.Append(Options.MarginTop);
arguments.Append(" ");
}
if (Options.MarginRight.HasValue)
{
arguments.Append("--margin-right ");
arguments.Append(Options.MarginRight);
arguments.Append(" ");
}
if (Options.MarginBottom.HasValue)
{
arguments.Append("--margin-bottom ");
arguments.Append(Options.MarginBottom);
arguments.Append(" ");
}
if (Options.PageWidth.HasValue)
{
arguments.Append("--page-width ");
arguments.Append(Options.PageWidth);
arguments.Append(" ");
}
if (Options.PageHeight.HasValue)
{
arguments.Append("--page-height ");
arguments.Append(Options.PageHeight);
arguments.Append(" ");
}
if (Options.HeaderSpacing.HasValue)
{
arguments.Append("--header-spacing ");
arguments.Append(Options.HeaderSpacing);
arguments.Append(" ");
}
if (Options.FooterSpacing.HasValue)
{
arguments.Append("--footer-spacing ");
arguments.Append(Options.FooterSpacing);
arguments.Append(" ");
}
if (!string.IsNullOrEmpty(Options.Header))
{
arguments.Append("--header-html ");
arguments.Append(Options.Header);
arguments.Append(" ");
}
if (!string.IsNullOrEmpty(Options.Footer))
{
arguments.Append("--footer-html ");
arguments.Append(Options.Footer);
arguments.Append(" ");
}
arguments.Append(url);
arguments.Append(" ");
arguments.Append(System.IO.Path.GetTempPath());
arguments.Append(filename);
startInfo.Arguments = arguments.ToString();
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
process.StartInfo = startInfo;
process.Start();
var errors = process.StandardError.ReadToEnd();
if (System.IO.File.Exists(System.IO.Path.GetTempPath() + filename))
{
byte[] pdf = System.IO.File.ReadAllBytes(System.IO.Path.GetTempPath() + filename);
return pdf;
}
return errors;
}
}
IServicePDF.cs
public interface IServicePdf
{
object GeneratePDF(string url, string filename);
PdfGlobalOptions Options { set; get; }
}
PdfGlovalOptions.cs
public enum PdfOrientation { Landscape, Portrait };
public class PdfGlobalOptions
{
public float? MarginTop { get; set; }
public float? MarginLeft { get; set; }
public float? MarginBottom { get; set; }
public float? MarginRight { get; set; }
public float? PageWidth { get; set; }
public float? PageHeight { get; set; }
public string Header { get; set; }
public string Footer { get; set; }
public float? HeaderSpacing { get; set; }
public PdfOrientation Orientation { get; set; }
public float? FooterSpacing { get; set; }
}
and lastly in your app config add the configuration necessary :
"wkhtmltopdf": {
"Path": "C:\\Program Files\\wkhtmltopdf\\bin\\",
"Program": "wkhtmltopdf.exe" }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.