import { jsPDF } from "jspdf";
import html2canvas from "html2canvas-pro";
@Component({
selector: "app-pdf",
template: `
<div #contentToExport>
<h1>Contenido a exportar</h1>
<p>Este es el contenido que deseas exportar como PDF.</p>
</div>
<button (click)="generatePDF()">Generar PDF</button>
`,
styleUrls: ["./pdf.component.css"],
})
export class PdfComponent {
@ViewChild('contentToExport') contentToExport!: ElementRef;
constructor() {}
generatePDF() {
const element = this.contentToExport.nativeElement;
html2canvas(element, { scale: 2, useCORS: true })
.then((canvas) => {
const imgData = canvas.toDataURL("image/png");
const pdf = new jsPDF("p", "mm", "a4");
const imgWidth = 208;
const imgHeight = (canvas.height * imgWidth) / canvas.width;
pdf.addImage(imgData, "PNG", 0, 0, imgWidth, imgHeight);
pdf.save("output.pdf");
})
.catch((error) => {
console.error("Error al generar el PDF:", error);
});
}
}
using System;
using System.Text;
using PdfSharpCore.Pdf;
using PdfSharpCore.Drawing;
public class Program
{
public static void Main()
{
// Ensure the encoding provider is registered
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
// Create a new PDF document
PdfDocument document = new PdfDocument();
document.Info.Title = "My PDF";
// Create an empty page
PdfPage page = document.AddPage();
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);
// Create a font
XFont font = new XFont("Verdana", 20, XFontStyle.Bold);
// Draw the text
gfx.DrawString("Hello, World!", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.TopLeft);
// Save the document
const string filename = "MyPDF.pdf";
document.Save(filename);
// Optional: Confirm successful creation
Console.WriteLine("PDF generated successfully using PdfSharp.");
}
}