Save HTML as PDF


Jackson, Adam
 

Hi,

 

xpa 2.5

 

Is there  a way of programmatically saving an HTML page as PDF? I have a pdf document that has links to other documents in it. I have those links stored in data so I want to produce a list of hyperlinks with friendly names and merge it with the main document. The references to the linked documents in the main document cannot be hyperlinks so I have gone down the route of generating HTML document of hyperlinks and then using COM to save it as a PDF. If I do it manually it all works but my COM commands are not quite right. It generates a PDF file with the correct name and extension but I get errors when I try to open it.

 

Open word

Get Documents object

Using Documents object’s Open method I open HTML file – I get a convert dialogue which I need to get rid of programmatically but I also get this when I do it manually

SaveAs2 – I set FileName argument as required. I have also played around with setting file format argument. I have tried wdFormatPDF and I have tried to use various trick with File Converters and FileConverter objects but pretty much get same result.

Close document

Quit Word

 

I think the file format argument on the SaveAs method is probably the key to this but I am not sure what to put in it.

 

Has anyone managed to do something similar or can see what I might be doing wrong?

 

TIA

 

Adam

 

 

 

 

 

Adam

Jackson

Senior Manager, Operations and Digital Systems

T  +447949892482

E  adam.jackson@...

 

 

McLaren Formula 1 Team

McLaren Technology Centre

Chertsey Road, Woking

Surrey, GU21 4YH, UK

McLaren60 celebrating our 60th year
 FOREVERFORWARD

mclaren.com

Facebook

Instagram

LinkedIn

Twitter

YouTube

 

The contents of this e-mail are confidential and for the exclusive use of the intended recipient. If you are not the intended recipient you should not read, copy, retransmit or disclose its contents. If you have received this email in error please delete it from your system immediately and notify us either by email or telephone. The views expressed in this communication may not necessarily be the views held by McLaren Racing Limited.
McLaren Racing Limited, McLaren Technology Centre, Chertsey Road, Woking, Surrey, GU21 4YH, UK. Company Number: 01517478


Craig Martin
 

You might have more success with an alternate approach using https://wkhtmltopdf.org/index.html
If you use python, it's easy to script with the pdfkit library - I've seen c# examples but never implemented it that way but I bet you could build a few lines of c# snippets in Magic to do your thing.

COM programming is so late Nineties.


From: main@magicu-l.groups.io <main@magicu-l.groups.io> on behalf of Jackson, Adam <adam.jackson@...>
Sent: Friday, March 3, 2023 5:59 AM
To: magicu-l@groups.io <magicu-l@groups.io>
Subject: [magicu-l] Save HTML as PDF
 

Hi,

 

xpa 2.5

 

Is there  a way of programmatically saving an HTML page as PDF? I have a pdf document that has links to other documents in it. I have those links stored in data so I want to produce a list of hyperlinks with friendly names and merge it with the main document. The references to the linked documents in the main document cannot be hyperlinks so I have gone down the route of generating HTML document of hyperlinks and then using COM to save it as a PDF. If I do it manually it all works but my COM commands are not quite right. It generates a PDF file with the correct name and extension but I get errors when I try to open it.

 

Open word

Get Documents object

Using Documents object’s Open method I open HTML file – I get a convert dialogue which I need to get rid of programmatically but I also get this when I do it manually

SaveAs2 – I set FileName argument as required. I have also played around with setting file format argument. I have tried wdFormatPDF and I have tried to use various trick with File Converters and FileConverter objects but pretty much get same result.

Close document

Quit Word

 

I think the file format argument on the SaveAs method is probably the key to this but I am not sure what to put in it.

 

Has anyone managed to do something similar or can see what I might be doing wrong?

 

TIA

 

Adam

 

 

 

 

 

Adam

Jackson

Senior Manager, Operations and Digital Systems

T  +447949892482

E  adam.jackson@...

 

 

McLaren Formula 1 Team

McLaren Technology Centre

Chertsey Road, Woking

Surrey, GU21 4YH, UK

McLaren60 celebrating our 60th year
 FOREVERFORWARD

mclaren.com

Facebook

Instagram

LinkedIn

Twitter

YouTube

 

The contents of this e-mail are confidential and for the exclusive use of the intended recipient. If you are not the intended recipient you should not read, copy, retransmit or disclose its contents. If you have received this email in error please delete it from your system immediately and notify us either by email or telephone. The views expressed in this communication may not necessarily be the views held by McLaren Racing Limited.
McLaren Racing Limited, McLaren Technology Centre, Chertsey Road, Woking, Surrey, GU21 4YH, UK. Company Number: 01517478


 

- download from wkhtmltopdf   (I downloaded the "7z Archive (XP/2003 or later)", and extracted it to "d:\temp\wkhtmltox"
- Make "D:\TEMP\wkhtmltox\bin" your current directory   (cd D:\TEMP\wkhtmltox\bin)
- Run: wkhtmltopdf.exe https://www.google.com google.pdf
- See attached PDF.


Adrian Wick
 

If you have devexpress:

using System;
using DevExpress.XtraRichEdit;
 
public static class Snippet
{
public static void func(System.String input_file, System.String output_file)
{
DevExpress.XtraRichEdit.RichEditDocumentServer server = new DevExpress.XtraRichEdit.RichEditDocumentServer();
server.LoadDocument(input_file);
server.ExportToPdf(output_file);
 
}
 
}
Easier to use c# than to use evaluate to do actions ... But it could also be done with evaluate.

Regards,
A